How come the result was like that? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0
19th Oct 2022, 3:24 PM
Mozzie
Mozzie - avatar
3 Answers
+ 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.
19th Oct 2022, 3:55 PM
Jayakrishna 🇮🇳
+ 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
19th Oct 2022, 6:08 PM
Apollo-Roboto
Apollo-Roboto - avatar
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.
20th Oct 2022, 5:29 AM
Roland
Roland - avatar