[Java] Equivalence comparison where beginners fail in string comparison. You may not even be aware of the mistake! ??

Intuitively, you'll want to use == when trying to determine if two strings are equivalent. However, in Java you have to use the ʻequals method instead of == `when comparing strings. In order to understand the reason for this, I think it is necessary to understand that ** strings are object types (reference types) in Java and the characteristics of Strings. ** **

Object type (reference type) variable

In the case of the object type, the required memory area is usually allocated from the class that is the blueprint of the object and the new operator is used to create the instance. And in Java, the object type is ** pointer **. In other words, the variable stores the address value of the memory where the actual value is stored, not the value. By the way, int and boolean are called primitive types, and this variable holds the value itself.

Creating a String object

String str = "hoge";

Normally, in the case of object type, the object is created using the new operator, but in the case of String, there is a convenient mechanism that can be created without using the new operator, so = It can be generated with just "" . Also, by generating with =" ", you can benefit from the mechanism called String Constant Pool described later.

String Constant Pool (Mechanism for reusing strings)

String Constant Pool is a mechanism to save memory when creating a new String object. The value is held in a special memory area called the String constant pool, and when a new string is generated, the pool is checked for the same string object, and if there is, the reference is reused. Will give you. To use this mechanism, create a String object with =" " without using the new operator. (There is also a way to explicitly use the new operator, but I will omit it.)

When using StringConstantPool


//Since this is the first time, the memory area is secured and generated as usual.
String str = "hoge";
//An existing reference because the same value already exists in the String constant pool(Address value)Connect with
String str_2 = "hoge";

When not using StringConstantPool


//Memory area is secured and generated as usual
String str = new String("hoge");
//Since String Constant Pool is not used, memory area is secured and generated as usual.
String str_2 = new String("hoge");

By the way, this is the reason why String is immutable. It seems that it is impossible to change it because changing one of them will affect all others when multiple same strings have the same reference.

It may be misunderstood that it is cool with ==

==Case that gets cool with


String str = "hoge";
String str_2 = "hoge";
//The condition of this if statement is true, but not because the values are the same, but the reference destination(Address value)Is the same.
if(str == str_2){
    System.out.println("The values are the same");
}else{
    System.out.println("The value is different");
}

Since the condition of the if statement in the above code is true, it may be illusion that the values can be compared, but this is a comparison of the reference destination (address value) and the String Constant Pool. It becomes true because the reference destination (address value) is the same due to reuse. Therefore, it does not hold in the following cases.

==Bad case


String str = "hoge";
String str_2 = new String("hoge");
//In this case, the reference is not reused, str and str_Reference of 2(Address value)Is different, so the condition is false.
if(str == str_2){
    System.out.println("The values are the same");
}else{
    System.out.println("The value is different");
}

How to compare values properly

To compare the values themselves, use the ʻequals method` as mentioned at the beginning.

String str = "hoge";
String str_2 = new String("hoge");
//You can compare the values themselves by using the equals method.
if(str.equals(str_2)){
    System.out.println("The values are the same");
}else{
    System.out.println("The value is different");
}

Summary

I have introduced a method for proper string comparison in the case of Java. I think the point is that the String type is an object type, and that the object type holds the reference destination (address value) instead of the value. If you can understand these two things, you can understand the reason for using the ʻequals method. By the way, in relatively new languages Swift and Kotlin, it seems that you can compare values with ==`.

reference

-String, heap area and constant pool --Qiita -[Java] What is a reference type? -Qiita -[Java] Why can string literals be compared by operators?-shogonir blog

Recommended Posts

[Java] Equivalence comparison where beginners fail in string comparison. You may not even be aware of the mistake! ??
The story of low-level string comparison in Java
Regarding String type equivalence comparison in Java
Summarize the life cycle of Java objects to be aware of in Android development
Feel the passage of time even in Java
Things to be aware of when writing code in Java
[Rails] Where to be careful in the description of validation
The story of not knowing the behavior of String by passing Java by reference
[Java] Speed comparison of string concatenation
[Java] Correct comparison of String type
Find the maximum and minimum of the five numbers you entered in Java
The story that the Servlet could not be loaded in the Java Web application
[Java] [Microsoft] Things to be aware of when including the JDBC driver for SQL Server in one jar