• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

equals vs ==

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess that, if the equals() test returns false, x and y can't possibly refer to the same object so x==y returns false.

If i am right then why in the following program the output turnsout to be "EQ".

Can anybody explain me.

1 public void testC(char ch) {
2 Integer ss = new Integer( ch );
3 Character cc = new Character( ch ) ;
4 if( ss.equals( cc )) System.out.println("equals");
5 if( ss.intValue() == cc.charValue() ){
6 System.out.println("EQ");
7 }
8 }

I know that the equals test fails when the objects are of different types
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For object comparsion, == always return false if they are not the same object as the memory addresses differ. However, as you are now comparing primitive types (one is int and the other is char, and char will be promoted to int), == only compares the values of the 2 objects, instead of the memory address, and thus, it returns true.

Nick
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nicholas is straightforwardly right.
intValue() & charValue() returns primitive datatype.

thanks
reply
    Bookmark Topic Watch Topic
  • New Topic