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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
tom
  • 5,114
  • 6
  • 24
  • 36
  • Override the `toString()` method to output `number`. – GriffeyDog Jun 11 '13 at 20:13
  • below is the out put iam getting com.Packagename.MyNumber@7c6768 com.Packagename.MyNumber@1690726 – tom Jun 11 '13 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](http://docs.oracle.com/javase/tutorial/) – informatik01 Nov 11 '13 at 01:12

3 Answers3

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"
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • 3
    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. – WilliamShatner Jun 11 '13 at 20:16
  • Might want to explain roughly what the hex digits, class name and '@' are doing. – Tom Hawtin - tackline Jun 11 '13 at 20:16
  • 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. – djangofan Jun 11 '13 at 20:19
  • 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 – tom Jun 11 '13 at 20:20
  • when i declare add methods as MyNumber(i.e with the constructor) what does it mean? – tom Jun 11 '13 at 20:21
  • It means you are creating new object with new value (addition or subtraction) and then returning it to the caller. Object that the method was called on is not modified. – Grzegorz Żur Jun 11 '13 at 20:28
  • Override annotation is just for new compilers (Java 7) to tell them that we wanted to override some method. If we by mistake make a typo (tooString), it will tell us that we are not overriding any method. – Grzegorz Żur Jun 11 '13 at 20:31
  • Thank you Grzegorz, i understood now – tom Jun 11 '13 at 20:36
  • @Grzegorz `@Override` has been around since Java 5, released in 2004, so it's not new at all. Its use on methods from interfaces was introduced in Java 6, but even that's from 2006, and that's not new either. – Brian Jun 11 '13 at 20:40
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);
}
Community
  • 1
  • 1
Piovezan
  • 3,215
  • 1
  • 28
  • 45
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());
       }
    }
z atef
  • 7,138
  • 3
  • 55
  • 50