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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

using == with Integer  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Report post to moderator
Dear all;Today I have another question for Using == with Integer.
in this example:
Integer x=400;
Integer y=x;
x++;
Sysout(x==y)
output is false.why?x refrence and y refrence point to same object.if so I think is when increase value in one refrence ;another refrence's value will be increased.
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Report post to moderator

Integer instances are immutable. You can't increment its value. When you apply the increment operator, the compiler will unbox, increment, and box the new result -- this process will refer to a different instance as the new value.

Henry
 
Greenhorn
Posts: 7
2
  • Mark post as helpful
  • send pies
  • Report post to moderator

saw mon wrote:Dear all;Today I have another question for Using == with Integer.
in this example:
Integer x=400;
Integer y=x;
x++;
Sysout(x==y)
output is false.why?x refrence and y refrence point to same object.if so I think is when increase value in one refrence ;another refrence's value will be increased.



This is the way I learned this:

increment x will result in 401



If you re-assigned after the increment then:

then you would get a true return.

y=x assigns the value of x to y
y==x checks to see if they are equal
 
saw mon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Report post to moderator

Henry Wong wrote:
Integer instances are immutable. You can't increment its value. When you apply the increment operator, the compiler will unbox, increment, and box the new result -- this process will refer to a different instance as the new value.

Henry



Dear Henry,
I wrote one sample program about Wrapper by following example.


In here,I found is that x++ is another Wrapper Object that value is 401 and x is refrence to That new Object.is it?why It print still 400?if I wrote like that x++ then when print x value ,it is 401.Why?I am confused.




 
saw mon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Report post to moderator

Henry Wong wrote:
Integer instances are immutable. You can't increment its value. When you apply the increment operator, the compiler will unbox, increment, and box the new result -- this process will refer to a different instance as the new value.

Henry




Dear Henry,
I wrote one sample program about Wrapper by following example.


In here,I found is that x++ is another Wrapper Object that value is 401 and x is refrence to That new Object.is it?why It print still 400?if I wrote like that x++ then when print x value ,it is 401.Why?I am confused.
 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator

Saw,

You seemed to have created a new topic for your followup question...

http://www.coderanch.com/t/661258/java-programmer-OCPJP/certification/Reference-Wrapper-Object

As such, I will closed this topic, as the original question has been answered.

Henry
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!