• 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

Is the following output correct ?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 38
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it..
Thank you Nabila and Meena
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic