• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

intuitive equals exam q, help

 
sarim raza
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 51.
Select the code segments(assuming is part of valid class) below that compile and run correctly with output: We are Equal
A.int i = 10;
long l = 10L;
if( i == l )
System.out.println("We are Equal");
B.int i = 10;
Integer ii = new Integer(10);
if( i == ii)
System.out.println("We are Equal");
C.int i = 10; char c = 10;
if( c == i)
System.out.println("We are Equal");
D.Integer ii = new Integer(10);
Integer jj = new Integer(10);
if(ii == jj)
System.out.println("We are Equal");
E.String s1 = "Null";
String s2 = "Null";
if( s1 == s2)
System.out.println("We are Equal");
F.String s1 = "Null";
String s2 = new String(s1);
if( s1 == s2)
System.out.println("We are Equal");
//I LIKED A AND E, BUT C IS ALSO CORRECT, HOW COME ?
IS IT BECAUSE OF THE NUMERIC PROMOTION DUE TO BINARY OPERATION ?
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yea, the char gets promoted to int before == operation occurs. I think this is the way. Please correct me if I am wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic