2

I am trying to learn more about constructors in Java.

Below is my code. I am trying to print an integer value (addition and subtraction) but my output is some random digits.

public class MyNumber {

    private int number;

    public MyNumber(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    public MyNumber add(MyNumber another) {
        return new MyNumber(this.number + another.number);
    }

    public MyNumber sub(MyNumber another) {
        return new MyNumber(this.number - another.number);
    }

    public static void main(String args[]) {
        MyNumber myNumber = new MyNumber(2);
        MyNumber c = myNumber.add(myNumber);
        System.out.println(c);
        MyNumber d = myNumber.sub(myNumber);
        System.out.println(d);
    }
}

This is my output:

com.Packagename.MyNumber@7c6768
com.Packagename.MyNumber@1690726

Please advise. I am very new to Java and any explanation is greatly appreciated.

3
  • Override the toString() method to output number. Commented Jun 11, 2013 at 20:13
  • below is the out put iam getting com.Packagename.MyNumber@7c6768 com.Packagename.MyNumber@1690726 Commented Jun 11, 2013 at 20:15
  • btw It would be useful for you to read Oracle's official Java tutorials, they are pretty good and quite easy to understand: The Java™ Tutorials Commented Nov 11, 2013 at 1:12

3 Answers 3

6

Override toString method so println outputs number you want.

@Override
public String toString() {
    return String.valueOf(number);
}

System.out.println calls toString() on object passed as argument, default implementation of toString() from class Object returns this com....ClassName@... thing. You need to override it as shown above.

More information in Java docs.

Moreover, toString() is called when operator + is applied on object being concatenated to String.

String a = "b" + new Integer(1); // "b1"
Sign up to request clarification or add additional context in comments.

9 Comments

This is the correct answer, but seeing as he is new, you might want to elaborate on why he would want to override the toString() method.
Might want to explain roughly what the hex digits, class name and '@' are doing.
I didn't realize that the default toString was related to the println output for the object but it makes sense because he is passing a object rather than a explicit string to println. I learned something here... thanks.
i did tryied using toString but i did not inclue(Override) the method in my class that is why it did not work now i added the methos and i could able to see the results as intgers..Thank you so much
when i declare add methods as MyNumber(i.e with the constructor) what does it mean?
|
3

Every Java object (for example myNumber) inherits from java.lang.Object and has a toString() method which is put to use when you call System.out.println(myNumber);. The toString() method in its default form prints the (apparently) random data you saw. In order to print the value of the number variable contained in the MyNumber class you simply override the toString() method like this:

@Override
public String toString() {
    return String.valueOf(number);
}

Comments

1
public class MyNumber {

       private int number;

       public MyNumber(int number){
           this.number = number;
       }
       public int getNumber(){
           return number;
       }
       public MyNumber add(MyNumber another){
           return new MyNumber(this.number + another.number);
       }
       public MyNumber sub(MyNumber another) {
              return new MyNumber(this.number - another.number);
           }
       
       public String toString() {
            return String.valueOf(number);
        }
       
       public static void main(String args[])
       {
           MyNumber myNumber = new MyNumber(2);
           MyNumber c = myNumber.add(myNumber);
           System.out.println(c.toString());
           MyNumber d = myNumber.sub(myNumber);
           System.out.println(d.toString());
       }
    }

1 Comment

Thank you so much i could able to figure this out with the help of Grzegorz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.