• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Regarding wrappers

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks uj.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic