• Post Reply Bookmark Topic Watch Topic
  • New Topic

Value of Reference Variables  RSS feed

 
Suhaas Mohandos
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below example the reference variable i is supposed to contain the memory address of 5, but it contains the value 5. Why?

I have more questions which I will ask once we progress, in this thread.


Output:
[Ljava.lang.String;@509f662e
5
Employee@10ed7f5c
 
Abhijeet Bhanjadeo
Greenhorn
Posts: 5
AngularJS Flex Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer is wrapper class for int and you are using the Constructor call to create a Integer Object with int value of 5.
Please Refer to: How AutoBoxing works?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Integer has overridden toString, so it returns the value of the object as a String.

The other two use the default toString of Object, which prints the hashcode() of the Object, which is usually some form of address (though this is not guaranteed in any way).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!