Languages
[Edit]
EN

Java - compare strings

3 points
Created by:
Yusef-Ewing
389

One of the most common problem that affects new Java Developers is how to compare two String objects. They try to compare strings with == operator what makes only reference comparision - not values. To solve this problem equals method should be used. This article explains how to compare two strings.

1. String.equals method example

public class Program {

    public static void main(String[] args) {

        String a = "text 1";
        String b = "text 1";
        String c = "text 2";

        // CORRECT WAY OF VALUES COMPARISON:

        System.out.println(a.equals(b)); // true
        System.out.println(a.equals(c)); // false

        // good practice because of literal on left site
        // NullPoiterException will not occur if a or c variables will be null
        System.out.println("text 1".equals(a)); // true
        System.out.println("text 1".equals(c)); // false
        
        System.out.println("text 1".equals("text 1")); // true
        System.out.println("text 2".equals("text 1")); // false

        // BAD PRACTICE BECAUSE OF VARIABLE ON LEFT SITE AND LITERAL ON RIGHT SITE:
        
        // possible NullPoiterException if a or c variable will be null
        System.out.println(a.equals("text 1")); // true 
        System.out.println(c.equals("text 1")); // false
    }
}

Note: this approach is available sice java 1.7.

Output:

true
false
true
false
true
false
true
false

2. Object.equals method example

import java.util.Objects;

public class Program {

    public static void main(String[] args) {

        String a = "text 1";
        String b = "text 1";
        String c = "text 2";

        System.out.println(Objects.equals(a, b)); // true
        System.out.println(Objects.equals(a, c)); // false
        System.out.println(Objects.equals(b, a)); // true
        System.out.println(Objects.equals(c, a)); // false

        System.out.println(Objects.equals(a, "text 1")); // true
        System.out.println(Objects.equals("text 1", b)); // true

        System.out.println(Objects.equals("text 1", "text 1")); // true
        System.out.println(Objects.equals("text 1", "text 2")); // false
    }
}

Output:

true
false
true
false
true
true
true
false

3. How to do not compare strings in Java

public class Program {

    public static void main(String[] args) {

        String a = "text 1";
        String b = "text 1";
        String c = "text 2";

        // INCORRECT WAY OF VALUES COMPARISON:
        
        // this examples return true because of reference to same object in memory
        System.out.println(a == b); // true
        System.out.println("text 1" == "text 1"); // true
    }
}

Output:

true
true

Note: for all "text 1" occurrences java uses same object in memory what caused true result during references comparison with == operator.

Alternative titles

  1. Java - how to compare strings?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - compare strings

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join