File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding wrappers

 
Venu Chakravorty
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following piece of code makes me think that: "i1" refers to "new Integer(5)" and i2 refers to the same "new Integer(5)" through "i1", i.e. "i2" refers to "i1" which refers to the "new Integer(5)". At the statement "i1++", "i1" gets unwrapped, incremented and rewrapped and refers to a different object, but "i2" still refers to "i1" (thus they refer to the same object), so the second "i1 == i2" should also return true, but it doesn't.


thanks and regards venu.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the statement "i1++", "i1" gets unwrapped, incremented and rewrapped and refers to a different object, but "i2" still refers to "i1" (thus they refer to the same object), so the second "i1 == i2" should also return true, but it doesn't.


You have to understand the distinction between reference and object.

i2 = i1 ... means assign the i2 reference to the object that is currently referenced by i1. So, after that operation both the i1 and i2 reference refers to the same object. If you change the i1 reference to something else, it doesn't automatically change the i2 reference.

Henry
 
Venu Chakravorty
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
venu chakravorty wrote:The following piece of code makes me think that: "i1" refers to "new Integer(5)" and i2 refers to the same "new Integer(5)" through "i1", i.e. "i2" refers to "i1" which refers to the "new Integer(5)".


What you're describing is an assignment by reference, but Java doesn't have that. In Java everything is done by value. Variables are assigned by value and parameters are passed by value. Nothing is done by reference. Still there word reference is used in Java but here it means "pointer to an object". You should be careful to make these distinctions,

* Java never assigns variables by reference. Java always assigns variables by value and the value is either a references (a pointer) or a primitive.

* Java never passes parameters by reference. Java always passes parameters by value and the value is either a references (a pointer) or a primitive.

So "by reference" is completely banned from Java. A reference in Java is a pointer.
 
Venu Chakravorty
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks uj.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic