aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why it is false? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Why it is false?" Watch "Why it is false?" New topic
Author

Why it is false?

Michael Rootman
Greenhorn

Joined: Apr 15, 2009
Posts: 19
Q69 from Inquisition exams:



The result is false, true false.

Why 2000==2000 is false ?

Thank you.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35249
    
    7
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.


Android appsImageJ pluginsJava web charts
Michael Rootman
Greenhorn

Joined: Apr 15, 2009
Posts: 19
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
Marshal

Joined: Mar 22, 2005
Posts: 35249
    
    7
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

Joined: Apr 15, 2009
Posts: 19
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"?
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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.



My Blog SCJP 5 SCWCD 5
Dejan Mratinkovic
Ranch Hand

Joined: Nov 20, 2008
Posts: 65

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

Joined: Apr 15, 2009
Posts: 19
HI,

Now I can understand the logic in the output.

Thank you to everyone for help!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Why it is false?
 
Similar Threads
Equality Problem
Please explain the answers...
Q on Integer()
Garbase Collection and Java Litral Pool
Integer == Integer?