jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about "=="and "equals" problem,I can Watch "about "=="and "equals" problem,I can New topic

about "=="and "equals" problem,I can't understand clearely?

Tu Ran
Ranch Hand

Joined: Feb 09, 2002
Posts: 30
When i use the Reference Equality Operators ==
,the explanation is "If the operands of an equality operator are both of either reference type or the null type, then the operation is object equality."
but how can I understand the code :
class why {
public static void main(String[] args) {
String s1=new String("Duke");
String s2=new String("Duke");
System.out.println("but s1!=s2");
the outcome is:
s1.equals(s2) return true;
s1==s2 return false?
//I think ,s1 and s2,pointed the same object:"Duke",in Equality Operators == ,the outcome is false?
[ February 13, 2002: Message edited by: Tu Ran ]
[ February 13, 2002: Message edited by: Tu Ran ]

Enjoy java,enjoy life
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
String s1=new String("Duke");
String s2=new String("Duke");
You are clearly instantiating TWO new String objects. I can tell because I see the word "new" written twice!!
Each new String object gets assigned to a different reference variable, s1 and s2. So clearly, these are two different objects, won't you agree?

If they are two different objects, they can NEVER be equivalent (ie, the same) so the expression s1==s2 will always be false.
It's like, my name is Rob. And I have a friend named Rob. But we are not the same person/object, are we?
Now, the equals() method of the String class, by definition, compares the internal character sequences stored in the String object. Since both objects are storing the same character sequences (Duke), the expression s1.equals(s2) is true.
It's like back to my friend and I. My name is Rob. His name is Rob. Are our names the same (equals())??
[ February 13, 2002: Message edited by: Rob Ross ]

SCJP 1.4
Tu Ran
Ranch Hand

Joined: Feb 09, 2002
Posts: 30
Thanks a lot~~~
I hava understanded what you tell me
"Reference Equality Operators == " and "equals" in
String class,thanks again。
But I want to know how I can get another information in detail about Java'Language,such as
"the equals() method of the String class, by definition, compares the internal character sequences stored in the String object. "
You know I just refer the book of JDKWINHelp
but I think it' so simple about these details;
Where can I get the details about these problem
in java?
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
You learn what methods do by reading the JavaDocs for them. JavaDoc is Sun's standard way of documenting all their classes.
You can read it online at:
When I look at the JavaDoc for String.equals(), here is the entry:

public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
When it comes to objects references, the '==' equal operator works by checking to see if the two references are pointing to the same object.
Objects created on the heap using the new() operator will be unique and have a difference memory address, thus the == operator will compare the address of the referenced object and will always return false as the following code snippet shows.
String s1 = new String("Same");
String s2 = new String("Same");
String s3 = s1;
String sa = "Same";
String sb = "Same";
System.out.println("s1 == s2 : " + (s1 == s2));
System.out.println("s1 == s3 : " + (s1 == s3));
System.out.println("sa == sb : " + (sa == sb));
System.out.println("s1.equals(sb) : " + s1.equals(sb));
output will be:
s1 == s2 : false
s1 == s3 : true
sa == sb : true
s1.equals(sb) : true
The equals() method compares each of the char literals for a match.
One pitfall to watchout for is the StringBuffer class, it also has a equals() methods, but unlike the wrapper classes like String, the equals() method is not overriden. So if,
StringBuffer s1 = new StringBuffer("Same");
StringBuffer s2 = new StringBuffer("Same");
s1.equals(s2) will be false
The answer is false because the StringBuffer.equals() method uses the default class Object method which compares object references!

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
"s1.equals(s2) return true;
s1==s2 return false?"
equals method in class String is overridden. equals method for class String compares the contents of the strings. Try for class StringBuffer ? (tip: for StringBuffer the method equals is NOT overridden). try it out and find out.....
wheras the operator " == " does the actual comparison to find whether the two objects are the same ones. (it is not content comparison here). but try for class StringBuffer. (also try the equals method for other wrapper classes like Long, Double etc)
I agree. Here's the link: http://aspose.com/file-tools
subject: about "=="and "equals" problem,I can't understand clearely?
It's not a secret anymore!