Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tu Ran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
if(s1.equals(s2))
System.out.println("s1.equals(s2)");
if(s1==s2)
System.out.println("s1==s2");
else
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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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())??
YES!
[ February 13, 2002: Message edited by: Rob Ross ]
 
Tu Ran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://java.sun.com/j2se/1.4/docs/api/index.html
When I look at the JavaDoc for String.equals(), here is the entry:

equals
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic