Two String objects can be compared using either == operator or equals() method of String class. But there is big difference in the meaning of both.
equals()
equals() method of String class check whether two strings have same CONTENTS or not. It does character by character comparison in order to check this.
For example,
String s="Sandy";
String s1="Sandy";
String s2="Mark";
System.out.println(s.equals(s1)); // will print true
System.out.println(s.equals(s2)); // will print false
s.equals(s1) is true because both have same contents.
equalsIgnoreCase()
It also checks whether two strings have same contents or not but it ignore the case when comparing the strings.
For example,
String s="Sandy"; // s in caps
String s1="sandy"; // small s
System.out.println(s.equals(s1)); // will print false
System.out.println(s.equalsIgnoreCase(s1)); // will print true
== operator
== is used to compare REFERENCES, not the contents of String objects. It checks whether two references are pointing to same String object or not.
For example,
String s="Sandy";
String s1="Sandy";
String s2="Mark";
System.out.println(s==s1); // will print true
System.out.println(s==s2); // will print false
here s==s1 is true not because their contents are equal. It is true because both s and s1 are pointing to same String object. == do not compare contents of strings.
Another Example:
String s3="Sandy";
String s4= new String("Sandy");
System.out.println(s3==s4); // will print false
Here s3 and s4 are pointing to two different objects therefore s3==s4 is false.
To see why s and s1 are pointing to same string objects, and s3 and s4 are different objects you can check here.
Similarly,
String s= new String("Sandy");
String s1= new String("Sandy");
System.out.println(s==s1); // will print false
This is because s and s1 are two different String objects.
equals() or == , which one to use?
Often beginners do mistake of using == when checking two strings for the equality of their contents.
Whenever you want to check whether two strings have same contents or not, you should use equals().
== should never be used for comparing contents of strings. It is only used to check whether two references are pointing to same object or not.
Let's see an example,
String s="String Testing";
String s1= new String("String Testing");
System.out.println(s==s1); // will print false
System.out.println(s.equals(s1)); // will print true
As you see that despite same contents s==s1 gives false because they are different objects. But s.equals(s1) gives true because they have same contents.
If you rely on == for comparing the contents of strings you may get wrong results.
== is faster
When you use equals() it first compares the references of two strings, then it does characater by character comparision of two strings.
While on the other hand == only compare references (pointers) and do not touch string characters. So obviously == is faster than equals().
Interned Strings and == operator
As you have read above that you should not use == for comparing contents of string but for interned strings you can use == to compare string contents also. This is because for interned strings same contents means same string objects.
This is described in detail in article Interned String and use of ==.
Some more examples:
Example 1:
"James Gosling" == "James "+"Gosling" will give true
Here "James Gosling" and "James"+"Gosling" are two same String objects.
This because "James" and "Gosling" will be combined at compile time to form a string "James Gosling"
Example 2:
String s= new String("String testing");
System.out.println(s== "String testing"); // false
This is false because "String testing" and s are two different objects with same contents. While
s.equals("String testing"); will give true here.
Example 3:
System.out.println( "Testing".equals("Testing")); // true
System.out.println( "Testing"=="Testing"); // true
equals()
equals() method of String class check whether two strings have same CONTENTS or not. It does character by character comparison in order to check this.
For example,
String s="Sandy";
String s1="Sandy";
String s2="Mark";
System.out.println(s.equals(s1)); // will print true
System.out.println(s.equals(s2)); // will print false
s.equals(s1) is true because both have same contents.
equalsIgnoreCase()
It also checks whether two strings have same contents or not but it ignore the case when comparing the strings.
For example,
String s="Sandy"; // s in caps
String s1="sandy"; // small s
System.out.println(s.equals(s1)); // will print false
System.out.println(s.equalsIgnoreCase(s1)); // will print true
== operator
== is used to compare REFERENCES, not the contents of String objects. It checks whether two references are pointing to same String object or not.
For example,
String s="Sandy";
String s1="Sandy";
String s2="Mark";
System.out.println(s==s1); // will print true
System.out.println(s==s2); // will print false
here s==s1 is true not because their contents are equal. It is true because both s and s1 are pointing to same String object. == do not compare contents of strings.
Another Example:
String s3="Sandy";
String s4= new String("Sandy");
System.out.println(s3==s4); // will print false
Here s3 and s4 are pointing to two different objects therefore s3==s4 is false.
To see why s and s1 are pointing to same string objects, and s3 and s4 are different objects you can check here.
Similarly,
String s= new String("Sandy");
String s1= new String("Sandy");
System.out.println(s==s1); // will print false
This is because s and s1 are two different String objects.
equals() or == , which one to use?
Often beginners do mistake of using == when checking two strings for the equality of their contents.
Whenever you want to check whether two strings have same contents or not, you should use equals().
== should never be used for comparing contents of strings. It is only used to check whether two references are pointing to same object or not.
Let's see an example,
String s="String Testing";
String s1= new String("String Testing");
System.out.println(s==s1); // will print false
System.out.println(s.equals(s1)); // will print true
As you see that despite same contents s==s1 gives false because they are different objects. But s.equals(s1) gives true because they have same contents.
If you rely on == for comparing the contents of strings you may get wrong results.
== is faster
When you use equals() it first compares the references of two strings, then it does characater by character comparision of two strings.
While on the other hand == only compare references (pointers) and do not touch string characters. So obviously == is faster than equals().
Interned Strings and == operator
As you have read above that you should not use == for comparing contents of string but for interned strings you can use == to compare string contents also. This is because for interned strings same contents means same string objects.
This is described in detail in article Interned String and use of ==.
Some more examples:
Example 1:
"James Gosling" == "James "+"Gosling" will give true
Here "James Gosling" and "James"+"Gosling" are two same String objects.
This because "James" and "Gosling" will be combined at compile time to form a string "James Gosling"
Example 2:
String s= new String("String testing");
System.out.println(s== "String testing"); // false
This is false because "String testing" and s are two different objects with same contents. While
s.equals("String testing"); will give true here.
Example 3:
System.out.println( "Testing".equals("Testing")); // true
System.out.println( "Testing"=="Testing"); // true
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment