Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is the following output correct ?

 
Manpreet Kaur
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Meena Ajay
Ranch Hand
Posts: 38
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Manpreet Kaur
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it..
Thank you Nabila and Meena
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic