• 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

Why it is false?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q69 from Inquisition exams:



The result is false, true false.

Why 2000==2000 is false ?

Thank you.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key to understanding this is to know how an "int" is converted to an "Integer" (a process called "autoboxing"). This is done by using the static Integer.valueOf(int) method. Its javadocs contain a very subtle hint what's going on. You may understand it if you consider these two questions: "Is 2 a frequently used value?" and "Is 2000 a frequently used value?"

Let us know of this helps; if not, we can go into more detail.
 
Michael Rootman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, sorry, I cannot say, that Javadoc make it more clear for me:

public static Integer valueOf(int i)

Returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.



Appreciate additional explanations.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public static Integer valueOf(int i)

Returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.


The hint is: "2" is a frequently requested value, while "2000" is not.
 
Michael Rootman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:
The hint is: "2" is a frequently requested value, while "2000" is not.



I am really sorry, but this does not make sense for ma at all. What is it "frequently requested value"? How should I know which one is and which is not?

Explanation in exam itself is not clear for me as well:

When the references i and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127.



How 127 is connected to comparing Integer values?
How JVM proceed boxing and unboxing, so same Integer values are equal or not equal depending on how this values big or "frequently requested"?
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Internally a cache is maintained to optimize space occupied by each Object. The source code reveals that the Integer class maintains an internal cache of Integers 0 to 127.

This is because you may frequently need the integers in this range.. Moreover this range also includes range of Byte and Short.

Here I am providing the code copied from the Integer class.

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

Please find here more details about
http://www.javabeat.net/articles/print.php?article_id=31

Section: 3)How ==, !=, <=, >= works for Boxing/Unboxing ?

Bottom line:


In order to save memory when primitive types are boxed into the wrapper types, the JVM allocates memory and creates a new object. But for some special cases, the JVM reuses the same object, two instances of the following wrapper objects will always be == when their primitive values are the same.

* Boolean
* Byte
* Character from \u0000 to \u007f (7f is 127 in decimal)
* Short and Integer from -128 to 127


 
Michael Rootman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

Now I can understand the logic in the output.

Thank you to everyone for help!
 
reply
    Bookmark Topic Watch Topic
  • New Topic