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,880 points
138 views

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";

System.out.println(s1.equals(s2));

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

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

// Evaluates to false
fooString1 == fooString2;

// Evaluates to true
fooString1.equals(fooString2);

// Evaluates to true, because Java uses the same object
"bar" == "bar";
answered Jul 12, 2018 by Daisy
• 8,050 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,190 points
    0 votes
    1. == This will always return false
    2. String1.equals(String2) returns true when both string are same.
    answered Jan 29 by Shashank
    • 1,350 points

    Related Questions In Java

    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,350 points
    90 views
    0 votes
    1 answer

    how to compare two words in java?

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

    answered Aug 7 in Java by sampriti
    • 1,100 points

    edited Aug 8 by Kalgi 395 views
    +5 votes
    3 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,380 points

    edited Nov 6, 2018 by Omkar 10,482 views
    0 votes
    2 answers

    How to create a 2-D array in java?

    int[][] multi = new int[5][]; multi[0] = new ...READ MORE

    answered Jul 16, 2018 in Java by Daisy
    • 8,050 points
    59 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 in Java by Sirajul
    • 39,540 points
    190 views
    +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,190 points
    65 views
    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,540 points
    54 views
    0 votes
    2 answers

    Integer to String conversion in java

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

    answered Jul 27, 2018 in Java by samarth295
    • 2,190 points
    83 views
    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 in Java by Sirajul
    • 39,540 points
    97 views
    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 27, 2018 in Java by samarth295
    • 2,190 points
    114 views