Pages

Tuesday, April 7, 2020

How To Compare Two Strings In Java? (With '==' Operator and equals() Method)

1. Introduction


In this tutorial, We'll learn how to compare two strings in java. Most of the developers use double equal operator "==" to compare two strings. My friend used "==" many times in his project but later faced an issue in production. Then he changed to String.equals() method.

Let us see what is the difference between "==" and equals() method in Strings Comparision.

2. Comparison with "==" Operator


Is this bad to use "==" operator for strings comparison. When this should be used?

Take a look at the following example program that uses "==" operator.


package com.java.w3schools.blog.java.program.to.strings;

public class StringsCompareDoubleEqualOperator {

 public static void main(String[] args) {

  String s1 = "java";
  String s2 = "java";

  String s3 = new String("java");
  String s4 = new String("java");

  System.out.println("(s1 == s2) : " + (s1 == s2));
  System.out.println("(s1 == s3) : " + (s1 == s3));
  System.out.println("(s2 == s3) : " + (s2 == s3));
  System.out.println("(s3 == s4) : " + (s3 == s4));

  String s5 = "javaprogramto.com";
  String s6 = "java" + "programto.com";

  System.out.println("(s5 == s6) : " + (s5 == s6));

  String s7 = s2 + "programto.com";

  System.out.println("(s5 == s7) : " + (s5 == s7));

  String s8 = new String(s7);
  System.out.println("(s7 == s8) : " + (s7 == s8));

  String s9 = s1;
  System.out.println("(s9 == s1) : " + (s9 == s1));

  String s10 = s8;
  System.out.println("(s10 == s8) : " + (s10 == s8));
 }

}

Output:

(s1 == s2) : true
(s1 == s3) : false
(s2 == s3) : false
(s3 == s4) : false
(s5 == s6) : true
(s5 == s7) : false
(s7 == s8) : false
(s9 == s1) : true
(s10 == s8) : true

"==" is used to reference equality (whether they are the same object). It compares only memory addresses of the two references but not the content. Check the output why false and true are returned by "==". If any two objects are pointing to the same memory address, it returns true otherwise false is returned.

3. Comparison with String equals() Method


String class has equals(Object anObject) method that takes Object as an argument. That means this method can take any type of object. This method is used to compare the contents of strings and will not compare the references. equals(Object o) is present as part of Object class and it compares the references. But, equals() is overridden in String class to compare the contents.



The below example has lots of comparisons all most returns true in many cases.

package com.java.w3schools.blog.java.program.to.strings;

public class StringsCompareEqualsMethod {

 public static void main(String[] args) {

  String s1 = "java";
  String s2 = "java";

  String s3 = new String("java");
  String s4 = new String("java");

  System.out.println("s1.equals(s2) : " + s1.equals(s2));
  System.out.println("s1.equals(s3) : " + s1.equals(s3));
  System.out.println("s2.equals(s3) : " + s2.equals(s3));
  System.out.println("s3.equals(s4) : " + s3.equals(s4));

  String s5 = "javaprogramto.com";
  String s6 = "java" + "programto.com";

  System.out.println("s5.equals(s6) : " + s5.equals(s6));

  String s7 = s2 + "programto.com";

  System.out.println("s5.equals(s7) : " + s5.equals(s7));

  String s8 = new String(s7);
  System.out.println("s7.equals(s8) : " + s7.equals(s8));

  String s9 = s1;
  System.out.println("s9.equals(s1) : " + s9.equals(s1));

  String s10 = s8;
  System.out.println("s10.equals(s8) : " + s10.equals(s8));

  String s11 = "Java";
  System.out.println("s1.equals(s11) : " + s1.equals(s11));

  String s12 = new String("javaprogramto.com");
  System.out.println("s5.equals(s12) : " + s5.equals(s12));

  String s13 = new String("JavaProgramTo.com");
  System.out.println("s13.equals(s12) : " + s13.equals(s12));
 }

}

Output:

s1.equals(s2) : true
s1.equals(s3) : true
s2.equals(s3) : true
s3.equals(s4) : true
s5.equals(s6) : true
s5.equals(s7) : true
s7.equals(s8) : true
s9.equals(s1) : true
s10.equals(s8) : true
s1.equals(s11) : false
s5.equals(s12) : true
s13.equals(s12) : false

If the contents of two strings are the same then it returns true. If not the same then it returns false.

If we pass other than String to equals() method then it returns false.

s1.equals(new StringBuffer("java"));

Even though the contents of s2 and StringBuffer are the same, it returns false because passed object is not a type of String. The given object is a type of String or not, this is done with "anObject instanceof String" condition.

If the contents are the same but it has an upper case or lower case, to ignore the case need to use equalsIgnoreCase(String anotherString) method from the String class.

s13.equals(s12) statement returned false earilier but if you change as s13.equalsIgnoreCase(s12) then returns true because equalsIgnoreCase() method ignores the case sensitives characters.

4. Conclusion


In this article, We've seen how to compare two strings and what is the differences between "==" VS equals() method.

"==" is to check reference equality
String "equals()" to compare two strings contents with case sensitive
String equalsIgnoreCase() to compare two string ignoring case types.

String contentEquals() method examples
https://www.javaprogramto.com/2019/03/string-contentequals.html

GitHub Code 1

GitHub Code 2

Reference API Ref

No comments:

Post a Comment

Please do not add any spam links in the comments section.