-4

In java, if we consider a class A, whose object we create by writing

A aObj = new A();

where aObj is the reference variable.

  1. Will printing this reference variable print the address of the object referred by it?
  2. As in the case of C, can '&' operator be used on reference variables to print their address?

Thanks!

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
Om Prakash
  • 33
  • 1
  • 7
  • 2
    Have a look at [this question](http://stackoverflow.com/questions/1961146/memory-address-of-variables-in-java?rq=1) and maybe [this one](http://stackoverflow.com/questions/7436581/what-is-the-difference-between-a-pointer-and-a-reference-variable-in-java?rq=1). – Till Helge May 07 '13 at 10:48
  • 4
    Why don't you try this? `System.out.println(aObj)` – Maroun May 07 '13 at 10:48
  • Ok looking at the answers below all I gotta say is don't rely on them. The objects hashCode method may or may not return the memory address. It's JVM implementation dependent. And even if it does most object will override this method. So any code relying on these methods to get the memory addresses will break more often than it will work. Anyway when you are working with Java you shouldn't be thinking of this. – Thihara May 07 '13 at 10:58

4 Answers4

4

Printing object will call the toString() method. If you don't override this method in your class, it will print ClassName@hashcode

ltebean
  • 1,019
  • 6
  • 15
1

If you have not overridden the toString() method in your object's class, it will invokes the default implementation defined in the Object class, which says:

The toString method for class Object returns a string consisting of the name of 
the class of which the object is an instance, the at-sign character `@', and the 
unsigned hexadecimal representation of the hash code of the object.

So, you will get the hashCode() representation of the Object , which may or may not be its address as it is implementation dependent. This is what Javadoc says about hashCode():

As much as is reasonably practical, the hashCode method defined by class Object 
does return distinct integers for distinct objects. (This is typically 
implemented by converting the internal address of the object into an integer, 
but this implementation technique is not required by the JavaTM programming 
language).
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

Printing the object will call toString() on the object and, if toString() is not overridden for that object, return a String like ClassName@Hexadecimal_Representation_Of_Hash_Code

For a class Test, the output will be something like:

Test@126b249

The hashCode is not necessarily the memory address and is implementation specific.

c.P.u1
  • 16,664
  • 6
  • 46
  • 41
  • Yes, I just tried that out and got a similar output. What happens if toString() is overriding that object?(I don't seem to get this part)? – Om Prakash May 07 '13 at 10:58
  • If class Test has a method `public String toString() { return "Test class custom toString method"; } System.out.println(new Test()); `would return "Test class custom toString method" – c.P.u1 May 07 '13 at 11:00
0

when you do

System.out.println(objReference)

toString() implementation is called. If you have overriden this, then your implementation will be called otherwise by default Object toString() implemtation will be called which prints hashCode of that object

If you look at String Class, it overrided the toString method , thats why when do

 System.out.println(stringObjectReference)

see the implementation here http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.toString%28%29

object refrence is not printed intead the actual value that object contains.

M Sach
  • 33,416
  • 76
  • 221
  • 314