• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Need help for Autboxing..

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does the (int) 128 be stored ?
I konw that only int values between -128 and 127 was stored as immutable objects.So i am confused why Integer k11 (= 128)== int k31( = 128) is true..


Thanks
sun junzhen
[ March 14, 2008: Message edited by: sun jiuzhen ]
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When comparing a wrapper with a primitive using == the wrapper is unboxed, thus Integer == int is evaluated as int == int.

This behavior is unlike comparing using equals() which causes the primitive to auto box. Integer.equals(int) is evaluated as Integer.equals(Integer).
[ March 14, 2008: Message edited by: Ahmed Yehia ]
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Integer wrapper class keeps track of a cache of 256 Integer objects in the range -128 to 127. If thru autoboxing a new Integer object is created for a value within that range, instead of creating a new instance, the reference to the cached instance is returned. Two Integer references can therefor point to the same object, which is why the == comparison succeeds.



Edit: Nevermind I completely missed the point of your question.
[ March 14, 2008: Message edited by: Jelle Klap ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


o/p: i3 == i4

O/P is as expected.

Something wrong with your sample code
 
Sun Craven
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all
But answer needs still...
-------------------
What does the int value 128 be stored?
----------------------------------------------
Thanks a lot
sun jiuzhen
[ March 14, 2008: Message edited by: sun jiuzhen ]
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.owasp.org/index.php/Java_gotchas

Please follow above link.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Craven",
Please check your private messages.
-Ben
 
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