• 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

Integer class doubt : ocp exams book

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

I learned that for "small" values, wrappers created through boxing will be kept in a pool. So, i5 and i6 (in the code below)
actually point to the same instance of Integer in the Integer pool. Hence, the output for set 3 is different from that of 1 and 2.

My doubt : What is the largest integer (created by boxing) that can be kept in a pool ?
Is there a similar concept for other wrappers ?

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

Rahul Sudip Bose wrote:
My doubt : What is the largest integer (created by boxing) that can be kept in a pool ?



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

Always remember that, if you do something like this, as it is also present in your set 2



It will always be a new object no matter the value it is storing is falling in range of Integer.
So for example like this




It will always yield false.

Arhaan
 
Sheriff
Posts: 9690
42
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
The range is -128 to 127...
 
Author
Posts: 375
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's code from Integer.java (Java API):



The 'pool' of Integer contains values from -128 to 127. If you use the assignment operator (=) and not the keyword 'new' to create Integer objects, Integer objects from this pool are reused.

cheers
Mala
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Do all other Wrapper classes for eg Double,Long etc use pool for storing values.

If yes,What are the ranges for their pool?
 
Mala Gupta
Author
Posts: 375
22
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay,

Float and Double wrapper classes do not define a 'cache' of values. All the other integral types (the ones that store integer values), i.e., Byte, Short, Integer, Long, Character do. Byte, Integer, Short, Long define values from -128 to 127 and Character from 0 to 127.

Here's the proof (partial source code from Java API):

Long.java



Short.java:



Byte.java:



Character.java:



cheers
Mala
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Considering the clarification given above, since i3 and i5 have been created through boxing and the value (21) given to them is also within the range of -128 to 127, i think it's quite logical that the expression i3==i5 is true. However, when I ran the code, it turned out to be false. Can anyone please explain?

Regards,
Nitin Sethi
 
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nitin sethi wrote:Hi All,

Considering the clarification given above, since i3 and i5 have been created through boxing and the value (21) given to them is also within the range of -128 to 127, i think it's quite logical that the expression i3==i5 is true. However, when I ran the code, it turned out to be false. Can anyone please explain?

Regards,
Nitin Sethi

Hi nitin sethi,

You said when code was run, it turned out to be false for i3==i5. But I checked it once by running the same code and it turned out to be true.. So once again check it .
 
nitin sethi
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prashanth,

Thanks for the reply.

There was a typo in my code.

Cheers,
Nitin Sethi
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic