• 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

Integer Caching?

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

Am I allowed to ask if Integer Caching is a required topic to be questioned on the OCPJP6 exam?

Thank you and Happy New Year.
Gary
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are allowed to ask and the answer is yes it can and most probably will be on the exam.

Happy New Year to you too
 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't find topic named "Integer caching" in kathy sierra and Bert bates book...... can you please tell me what is it ???
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically its a pool of Integer objects in the range -128 to 127 which are returned when you autobox int to Integer...
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it maintained for each wrapper class ??? e.g Short,Byte etc ... ???
 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankur trapasiya wrote:Is it maintained for each wrapper class ??? e.g Short,Byte etc ... ???



It's maintained for:
Boolean
Byte
Character from \u0000 to \u007f
Short and Integer from -128 to 127

And it's used when comparing two wrapped values such:


The code above will print:
same object
meaninfully equal

Take care to do not mistake a comparision as follows:


I hope this can make it clear.
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, thanks. This is NOT covered in the K&B V6 book (or V5 book).


It looks like this wrapper pool operates the same way as the String constant pool.

Gary

========

Well, I just found where this IS covered in the K&B book: chapter 3 "Assignments", page 246, however the authors do not refer to this as Integer caching. They just explain the concept.

Gary
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gary,

you can get more about it at Java Language Specification - topic 5.1.7 as follows:

5.1.7 Boxing Conversion


... If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.



At this topic there is a session called discussion that describes more:


Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly.

For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.

This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all characters and shorts, as well as integers and longs in the range of -32K - +32K.



Hope it can help.
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adolfo:

Thank you for this. After reviewing your posted text and going over and over the text in the K&B book, I understand that caching of certain wrapper objects will occur. Further, I understand that the Character, Short and Integer wrapper objects will be cached IF their given values are within a certain range. For example, for Short and Integer that range is -128 to +127. If the given value of a Short or Integer object is outside of this stated range of values then caching of that Short or Integer object will NOT occur, and therefore, if their given values are the same, "==" on those objects will be "false" and ".equals" (dot equals) will be "true".

Am I understanding this correctly?

Thank you for your time and your patience

Gary
 
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

Gary Marshall wrote: I understand that caching of certain wrapper objects will occur. Further, I understand that the Character, Short and Integer wrapper objects will be cached IF their given values are within a certain range. For example, for Short and Integer that range is -128 to +127. If the given value of a Short or Integer object is outside of this stated range of values then caching of that Short or Integer object will NOT occur, and therefore, if their given values are the same, "==" on those objects will be "false" and ".equals" (dot equals) will be "true".

Am I understanding this correctly?



Incorrect. The JLS specifies the data types and range values that is to be cached. It does *not* state what happens to the data types and values outside of the range. It is within specification for an implementation to cache data types and ranges larger than required.

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

What important to note here is some unusual behavior because of Integer Cacheing and auto boxing/unboxing. I have read somewhere that some JVM caches integer value upto -127 to 127 and creates same object through autoboxing e.g.

Integer i1=20;
Integer i2 =20;

i1==i2; //will print equal if JVM caches Integer

while

Integer i3=320;
Integer i4=320;

i3=i4; //will print unequal.

thogh It looks to me JVM specific because in my JRE 6 JVM it always print equal in both cases but its worth noting this point.
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The JLS specifies the data types and range values that is to be cached. It does *not* state what happens to the data types and values outside of the range. It is within specification for an implementation to cache data types and ranges larger than required.



OK. By "larger than required" are you referring to, for example, an Integer whose given value is less than -128 or greater than +127? Are saying that the JVM will cache a data element when its given type and range is outside of a specified range, meaning that an Integer type that has a value of 129 will be cached? And then another Integer type with the identical value of 129 will always be "=="?

Respectfully, your response is confusing to me.

Thank you
Gary
 
Henry Wong
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

Gary Marshall wrote:
OK. By "larger than required" are you referring to, for example, an Integer whose given value is less than -128 or greater than +127? Are saying that the JVM will cache a data element when its given type and range is outside of a specified range, meaning that an Integer type that has a value of 129 will be cached? And then another Integer type with the identical value of 129 will always be "=="?



No. Please read my answer again.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic