*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is the following output correct ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is the following output correct ?" Watch "Is the following output correct ?" New topic
Author

Is the following output correct ?

Manpreet Kaur
Greenhorn

Joined: Jul 03, 2008
Posts: 8
The following question is from http://www.geekevaluation.com/

public class etattva5 {
public static void main(String[] args) {
Integer i = 200;
Integer j = 200;
System.out.print(i==j);
System.out.print(i.equals(j));
}
}

What will be output for the above program?


1. false false
2. false true
3. true true
4. true false

Correct AnswerExplanation

false true
integer prec limit,it will give true true till 127 no after that.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Manpreet Kaur:
The following question is from http://www.geekevaluation.com/

public class etattva5 {
public static void main(String[] args) {
Integer i = 200;
Integer j = 200;
System.out.print(i==j);
System.out.print(i.equals(j));
}
}

What will be output for the above program?


1. false false
2. false true
3. true true
4. true false

Correct AnswerExplanation

false true
integer prec limit,it will give true true till 127 no after that.


Yeah , what's mentioned is perfectly correct.
Till the point Integer value <= 127 , both the Integer objects will have the same value as well as the same reference.
Only one object is created for both the statements.

However, here you haev Integer = 200.
Which is more then 127 .
In that case , there are separate objects created for both.
Therefore,
System.out.print(i==j);
will be False ( They are not the same objects)

and System.out.print(i.equals(j));
will be True - they have the same value ie.200



Hope that helps.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Meena Ajay
Ranch Hand

Joined: May 28, 2008
Posts: 38

Hi..
The answer is false true
According to the explanation given in K&B,
"In order to save memory, 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
"

Since the values of i and j exceed 127 (i.e 200) they are created as 2 separate wrapper objects, with different references.
That is why, the == comparison returns false, whereas the equals comparison which looks for the primitive value/content returns true.
[ July 08, 2008: Message edited by: Meena Subramanian ]

Cheers, Meena
OCPJP 6
Manpreet Kaur
Greenhorn

Joined: Jul 03, 2008
Posts: 8
Got it..
Thank you Nabila and Meena
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Manpreet

Answer will be false true .

i==j will return false bacause both i and j are greater than 200 hence they will refer to distinct objects .

i.equals(j) will return true because both objects have of same type and they have equal values.


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Also, keep in mind, that the specification states that the integer cache has to cache values to 127. It does *not* state that values bigger than 127 should not be cache.

This means, in the future, it is possible for a version of Java (or an implementation of Java 5) to return "true true", and that implementation is considered valid.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Is the following output correct ?
 
Similar Threads
question on Boolean objects
Integer
please explain
please explain the concept behind this
Can anyone help me with the explanation of this output ???