• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper Classes ,== and !=

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in K&B page No 245,246

we have a something whicha says Inorder to save memory,two instances of the wrapper objects(created through boxing) will always be == when their primitives values are equal.

they also gave example like this



ant hey have mentioned out put will be equal follwed by not equal.

I am not clear abt this one ,can some one help me on this
 
author
Posts: 23951
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

Suresh Babu Venkat wrote:
ant hey have mentioned out put will be equal follwed by not equal.



Obviously, you are quoting something out of context, as it is not possible for two objects to be both equal and not equal, with a reference comparison.

Henry
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

both the object are equal............
"not equal" will not get printed..........
when we do Integer a=10
and then Integer b=10//this will search for 10 whether it is available on the constant pool or not....
if yes then it will refer the same which is available
so as "==" checks whether two refernce varaiable refer to the same object,,,,,,
as the both refer to the same object so they are equal...........

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same


applies when you creating the instances like the OP's code.

Example,
 
Suresh Babu Venkat
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everyone for the help

actually in the book for not equal condition they have created wrappers with value 1000 ,so it says not equal

where as for equals condition is checked with values 10 , so that too passes.

 
Suresh Babu Venkat
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Prasad Kharkar wrote:Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same


applies when you creating the instances like the OP's code.

Example,



Prasad Kharkar wrote:Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same



in that case the given code below


should print not equal according to the rule which you have mentioned as this is Long.But it is returning equal.

the same code prints notequal if we give value as more than 127 .

so does that rule apply also to Longs?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suresh Babu Venkat wrote:so does that rule apply also to Longs?


Yes
 
Suresh Babu Venkat
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i know the reason behind this one...

in K&B it is mentioned just to save memory, can some one tell more about the reason.How will this behavior save memory?
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just putting my opinion here experts will comment if I am wrong
I don't know exact reason but that can be true

the reason is that those objects are stored in the constant pool like String objects
so when two objects are created of same value then the two reference variables refer to the same objects
like Strings
hope this helps

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:the reason is that those objects are stored in the constant pool like String objects


No. Integer caches the values from -128 to 127.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does caches mean?
I am not aware about it
please explain
 
Suresh Babu Venkat
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:

Prasad Kharkar wrote:the reason is that those objects are stored in the constant pool like String objects


No. Integer caches the values from -128 to 127.



what about Longs?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suresh Babu Venkat wrote:

Darryl Burke wrote:

Prasad Kharkar wrote:the reason is that those objects are stored in the constant pool like String objects


No. Integer caches the values from -128 to 127.



what about Longs?



What happened when your tried the same code but replaced Integer with Long ?
 
Suresh Babu Venkat
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:

Suresh Babu Venkat wrote:

Darryl Burke wrote:

Prasad Kharkar wrote:the reason is that those objects are stored in the constant pool like String objects


No. Integer caches the values from -128 to 127.



what about Longs?



What happened when your tried the same code but replaced Integer with Long ?



i have already mentioned what happened , it behaving same ...see the following quote where i have mentioned





should print not equal according to the rule which you have mentioned as this is Long.But it is returning equal.

the same code prints notequal if we give value as more than 127 .

 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suresh Babu Venkat wrote:

Deepak Bala wrote:

Suresh Babu Venkat wrote:

Darryl Burke wrote:

Prasad Kharkar wrote:the reason is that those objects are stored in the constant pool like String objects


No. Integer caches the values from -128 to 127.



what about Longs?



What happened when your tried the same code but replaced Integer with Long ?



i have already mentioned what happened , it behaving same ...see the following quote where i have mentioned





should print not equal according to the rule which you have mentioned as this is Long.But it is returning equal.

the same code prints notequal if we give value as more than 127 .




I missed that. But upon further perusal it appears your question was answered. Yes it does apply to Long and a few other Wrappers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic