[Java] Correct comparison of String type

Let's dig deeper into String type comparison

Organized the comparison of String type values.

Equal comparison method

  1. == -For primitive types, compare values. -In the object type, the reference values of the objects are compared and it is judged whether the objects are the same.

  2. equals() It can be used only in the object type, and the contents of the objects are compared to determine whether the objects have the same value.

In other words, Strng type is a reference type object, so you can use equals.

Verification (Is it really impossible to compare with ==)


    String str1 = "A";
    String str2 = "A";
    String str3 = new String("A");
    if(str1 == str2) {
    }else {
    if(str1 == str3) {
    }else {



It became TRUE. Is it okay to compare the String type with ==?


A value is created in the heap area when the String type is initialized, but it seems that it will be reused if the same value is used.

I tried to illustrate


To save memory, use the area (address = 100) where the value "A" matches without new.

Behavior when changing the value of String


When you rewrite the value of a variable, the reference source value (address = 100) is not changed, but the new area of "B" is new and the reference destination is changed. (Changed the reference address from 100 to 200)

What is the strings object equals doing


    private final char value[];

    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;                //Value of own object
                char v2[] = anotherString.value;  //Value of the compared object
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])           //Decompose String into char and compare values
                        return false;
                return true;
        return false;

The String object is decomposed into char [] and the values are compared one by one.


If the String object has the same value to save memory, the reference value (reference address) may be the same for different local variables, but it is not guaranteed, so compare with equals.

Recommended Posts

[Java] Correct comparison of String type
[Java] Comparison of String type character strings
[Java] Speed comparison of string concatenation
Regarding String type equivalence comparison in Java
[Java] String comparison and && and ||
The story of low-level string comparison in Java
[Java] Type conversion speed comparison
[Null safety] Kotlin Java comparison memo Correct use of Null safety
Various methods of Java String class
Java string
Initialization with an empty string to an instance of Java String type
[Java] Implicit type cast (AOJ10-sum of numbers)
Java date data type conversion (Date, Calendar, String)
[Java] Data type / string class cheat sheet
[Java] Convert Object type null to String type
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Basic knowledge of Java] About type conversion
Java Summary of frequently searched type conversions
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
Type conversion from java BigDecimal type to String type
[JAVA] Stream type
[Java] String padding
MyBatis string comparison
[Java] Enumeration type
Java Optional type
Java double type
[Java] Map comparison
Java string processing
Java framework comparison
Split string (Java)
[Java] Overview of Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Java] Be careful of the key type of Map
Java type conversion (String, int, Date, Calendar, etc.)
Why Java String typeclass comparison (==) cannot be used
Java review ③ (Basic usage of arrays / reference type)
Expired collection of java
Predicted Features of Java
[Java] Meaning of Public static void main (String [] args)
NIO.2 review of java
Java version notation comparison
[Java] String join execution speed comparison (+ operator vs StringBuilder)
[Java] Data type ①-Basic type
Java string multiple replacement
[java.io] Summary of Java string input (InputStream, Reader, Scanner)
[Java, Kotlin] Type Variance
Java class type field
History of Java annotation
[Note] Java: String search
Type determination in Java
Java study # 1 (typical type)
[Note] Java: String survey
java (merits of polymorphism)
[Java] About Objects.equals () and Review of String comparisons (== and equals)
About Java String class
GetXxxx of ResultSet was addicted to primitive type (Java)
[Java] One type of alphabet prohibited FizzBuzz with binding [Binding]
NIO review of java
[Java] About enum type
Equivalence comparison of Java wrapper classes and primitive types