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

Recommended Answers

All 4 Replies

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

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.