Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Need quick summary of == and equals()

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya all,
Passing the exam Friday, I need a quick summary for == and equals() for Objects, Strings or anywhere else they can lead to confusion. Thanks
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals() method is a method in the Object class. It does exactly the same as ==. However, a lot of classes have overridden this method so that it performs additional comparisons. The important ones where this is the case is the String class and the wrapper classes (Integer, Short, Long, etc). In these, the method has been overridden so that it's objectA.value().toSting() == objectB.value().toString() rather than saying are they both the same object (which is what == does).
 
Greg Georges
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now if I remember well, the equals() method and == operator compare two see if 2 references are the same object, if not false is returned, true otherwise.
In String class, equals() method checks to see if it is the same object, however == checks to see that the contents of the string are the same eg. "Hello" == "Hello".
The wrapper classes eg Boolean, returns true if both references are the same type, lets say two references are either true or false.
Is this info correct, anything missing?
 
Trevor Green
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You wrote this:
In String class, equals() method checks to see if it is the same object, however == checks to see that the contents of the string are the same eg. "Hello" == "Hello".
This is the wrong way round.
if you have the following objects:
String s1 = "me";
String s2 = "you";
String s3 = "me";
String s4 = s1;
Then the following lines all return true:
s1 == s4;//because s1 and s4 are the same object
s1 != s3;// s1 and s3 are different objects == compares to see if they're the same object
s1.equals(s3);//String.equals() compares the value of the string so they're the same.
!s1.equals(s2);//values are obviously different.
 
Trevor Green
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...opps. Actually, the above is not strictly true, where I've put String s1 = "me"; this should be String s1 = new String ("me"); ...and the same for the others.
Objects in the string pool act slightly different (you can't have duplicates and are therefore the same object, whereas if you use the new keyword then you always create a new object). Sorry for any confusion.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg,
Seems you are still confused about a few key concepts. I suggest that you try to get your hands on a copy of "Practical Java" by Peter Haggar and read the chapter on "Objects and Equality" particularly Praxis 8, 9, 10. These will be really helpful for the exam.
Meanwhile, you can read Praxis 8 online here: http://www-106.ibm.com/developerworks/java/library/praxis/pr8.html
 
Greg Georges
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, think I got it. I must think of memory references. If I have
String firststring = "Hello";
String secondstring = "Hello";
Becuase they are the same in composition, Java conserves ressources by reusing the identical strings. Therefore in this case (firststring == secondstring) returns true
However, if we force Java to create new String objects
String firststring = new String("Hello");
String secondstring = new String("Hello");
(fisrtstring == secondstring) returns false because it is obvious that the two objects are in different locations in memory.
 
Greg Georges
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything is perfect now, thanks guys, decided to code a couple examples to make sure, anyways this should no longer get me on the exam.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to try the self review q's on Object Equivalence at my site
http://www.tipsmart.com/studytools/selfreview/objequiv.htm
-Sandeep
 
Don't touch me. And dont' touch this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic