• 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

equals method

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Which of the following prints "Equal"

A) int a = 10; float f = 10;
if ( a = = f) { System.out.println("Equal");}
B) Integer i = new Integer(10);
Double d = new Double(10);
if ( i = =d) { System.out.println("Equal");}
C) Integer a = new Integer(10);
int b = 10;
if ( a = = b) { System.out.println("Equal");}
D) String a = new String("10");
String b = new String("10");
if ( a = = b) { System.out.println("Equal");}

answer is A.Please explain.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answers B, C, and D are all using == to compare object references, so they will certainly not print "Equals".

The reason that answer A is correct is because the implicit casting rules for the == operator are the same as the arithmetic operators. In other words, if you had written

a + b

the compiler would have tried to cast to whatever type a is.

a == b works the same way.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
->A and C are correct answers.

->B will give compiler error, you can't compare different Wrapper class
objects, using == operator. There will be incompatibility error.

->D will result false result, you are comparing two String object references, objects are created using new operator.

"A" refers to simple contract, even if the primitives are different but having
same value, they will be ==.

"C": after unboxing the comparison is done, resulting true.


Note: Why did you place a space between ==. It is == and not = =.
= = will give compiler error.


Thanks,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill Cruise


Answers B, C, and D are all using == to compare object references, so they will certainly not print "Equals".




Bill, are you sure C will not print "Equal"?

== operator will be applied after unboxing because one of the operand is
primitive. "a" will be unboxed, yielding value 10, so therefore there will
be comparison between two primitives that are having same values.


Thanks,
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,

You're absolutely right. I didn't account for autoboxing.
 
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
"ketki kalkar",

Please Quote Your Sources.

Thanks,
Henry
 
Nothing? Or something? Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic