How to compare Strings in Java

0 votes
I have an array of Strings. I want unique Strings in that array. How to check the equality between two Strings?
Apr 12, 2018 in Java by Sushmita
• 6,910 points

5 answers to this question.

+1 vote

In Java, Objects.equals() method is used to check the equality between Strings.

Objects.equals() method checks for the null before calling .equals()

String s1="Hello";

String s2="Hello";


Output of this code will be true. If the Strings are not equal, Output will be false.

answered Apr 12, 2018 by Parth
• 4,630 points
+1 vote
String fooString1 = new String("foo");
String fooString2 = new String("foo");

// Evaluates to false
fooString1 == fooString2;

// Evaluates to true

// Evaluates to true, because Java uses the same object
"bar" == "bar";
answered Jul 12, 2018 by Daisy
• 8,120 points
0 votes

There are two ways to compare strings in Java :

  • Use equals() 
    • If both strings are same the equals() method will return "true"  and else it will return "false".
    • This method is case sensitive, thus if you compare "String" & "string" it will return false.
    • You can use equalsIgnoreCase() in above case.
  • Use "==" operator
    • This operator compares the objects, not values.
    • It is Case sensitive.

There is another method compareTo() which compares strings lexicographically.

  • string1 == string2 : return 0
  • string1 > string2   : return any positive value
  • string1 < string2   : return any negative value

    answered Jul 13, 2018 by Mrunal
    • 680 points
    0 votes

    The == operator check if the two references point to the same object or not. .equals() check for the actual string content (value).

    Note that the .equals() method belongs to class Object (super class of all classes). You need to override it as per you class requirement, but for String it is already implemented, and it checks whether two strings have the same value or not.

    • Case 1

      String s1 = "Stack Overflow";
      String s2 = "Stack Overflow";
      s1 == s2;      //true
      s1.equals(s2); //true

      Reason: String literals created without null are stored in the String pool in the permgen area of heap. So both s1 and s2 point to same object in the pool.

    • Case 2

      String s1 = new String("Stack Overflow");
      String s2 = new String("Stack Overflow");
      s1 == s2;      //false
      s1.equals(s2); //true

      Reason: If you create a String object using the new keyword a separate space is allocated to it on the heap.

    answered Jul 24, 2018 by samarth295
    • 2,220 points
    0 votes
    1. == This will always return false
    2. String1.equals(String2) returns true when both string are same.
    answered Jan 30, 2019 by Shashank
    • 1,370 points

    Related Questions In Java

    0 votes
    0 answers

    how to compare two strings in java?

    how to compare two strings in java? READ MORE

    Nov 16, 2021 in Java by Naresh
    • 210 points
    0 votes
    1 answer

    How to split Strings by space in Java ?

    You can use split() method. str = "Hello ...READ MORE

    answered May 16, 2018 in Java by sharth
    • 3,370 points
    0 votes
    1 answer

    how to compare two words in java?

    hey,  I think in your code the function ...READ MORE

    answered Aug 8, 2019 in Java by sampriti
    • 1,120 points

    edited Aug 8, 2019 by Kalgi 2,108 views
    +5 votes
    4 answers

    How to execute a python file with few arguments in java?

    You can use Java Runtime.exec() to run python script, ...READ MORE

    answered Mar 27, 2018 in Java by DragonLord999
    • 8,450 points

    edited Nov 7, 2018 by Omkar 80,283 views
    0 votes
    2 answers

    How can I convert a String variable to a primitive int in Java

     Here are two ways illustrating this: Integer x ...READ MORE

    answered Aug 20, 2019 in Java by Sirajul
    • 59,230 points
    +1 vote
    2 answers

    How to generate random integers within specific range in Java?

    You can achieve that concisely in Java: Random ...READ MORE

    answered Jul 25, 2018 in Java by samarth295
    • 2,220 points
    0 votes
    1 answer

    How to divide a string in two parts

    String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

    answered Apr 13, 2018 in Java by Rishabh
    • 3,620 points
    0 votes
    2 answers

    Integer to String conversion in java

    We can do this in 2 ways: String ...READ MORE

    answered Jul 28, 2018 in Java by samarth295
    • 2,220 points
    0 votes
    2 answers

    How to round any number to n decimal places in Java?

    new BigDecimal(String.valueOf(double)).setScale(yourScale, BigDecimal.ROUND_HALF_UP); will get you a BigDecimal. To ...READ MORE

    answered Aug 26, 2019 in Java by Sirajul
    • 59,230 points
    0 votes
    3 answers

    How to read a Text File in Java?

    You can use readAllLines and the join method to ...READ MORE

    answered Jul 28, 2018 in Java by samarth295
    • 2,220 points
    webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP