0

Just a begginner question


Object x = new Object();
System.out.println(x);

everytime i get sthg.
16:18:35,222 INFO [STDOUT] --java.lang.Object@adfaec
16:19:26,937 INFO [STDOUT] --java.lang.Object@181db77
16:21:34,538 INFO [STDOUT] --java.lang.Object@14e5e21


what is the thing being printed? the object memory place? please help a beginner

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by jon.kiparsky
0

adfaec, 181db77, etc. these are memory addresses of the object that's created. java.lang.Object is the fully qualified name of the class.

0

Aha, thank you very much. So they are always unique.If i create new object, it will be allocated a new memory address and not old one.

0

The background for what you see is that when the compiler seeing an object item in the println() method call, it calls the object's toString method. The default toString method for the Object class returns: typename@hashcode where hashcode default value is the memory location of the object

0

Aha, thank you very much. So they are always unique.If i create new object, it will be allocated a new memory address and not old one.

I don't think you can rely on that being true. It'll be true as long as you don't let any of your Objects go out of reference, I suppose, but when garbage collection happens and those addresses are reclaimed, they can be recycled. So it's certainly possible to find that you're not getting unique addresses for your Objects in the life of an ordinary program, unless you hold on to all of those references.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.