0
How come the result was like that?
3 ответов
+ 3
You are printing obj reference in memory , ie memory address of the object.
When you print an object, toString() method of class is automatically called and it's default implementation returns class_name@address.
+ 3
If you want to have your own display of an object, you can define a .toString() method
example:
public class Coordinate {
public double x;
public double y;
public Coordinate(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
Coordinate coord = new Coordinate(4,6);
System.out.println(coord);
(4, 6) will be printed instead of Coordinate@1234abcd
0
If your question is 'why the references are same'? Is because the ref in constructor is the ref of the instance you made of the object.
Try to make an other instance to understand.