Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic