-1
class Tank{
    int level;
}
class aliasing{
    public static void main(String args[]){
        Tank t1 = new Tank();
        Tank t2 = new Tank();

        t1.level=21;
        t2.level=32;
        System.out.println("t1: " + t1 + " t2: " + t2);
    }
}

This block of code produces the output: t1: Tank@1b4b24d t2: Tank@260829. Obviously this is wrong, but i don't know why all of a sudden all my code is producing nosense. Also, if i just intiliaze a primitive value i can print that out no problem with the correct value, so i don't know why only objects are messing up.

2 Answers2

1

You need to override the toString() method in Tank class to produce meaningful representation of your object.

Kakarot
  • 4,252
  • 2
  • 16
  • 18
0

It is printing the correct output. As can be seen in Object.toString() documentation, the default is to print the name of the class, an @ character and the hash of the specific object.

The hashes both differ as they are two different objects. If you were to set t1 = t2 = new Tank(); then you would see the hashes match up as well.

Valter Jansons
  • 3,904
  • 1
  • 22
  • 25