November 11, 2020 In yesterday's article, I dealt with the content [Java] == and equals difference. Today, I'll summarize how to compare case-insensitively with the equals method and how to compare nulls with the equals method of the Objects class.
==
)Used to compare whether two strings are equal. Primitive types such as int type and double type are compared with ==
, but String type is a reference type, so compare using equals method.
public static void main(String[] args) {
//Initialize the String type variables str1 and str2 with the same string
String str1 = "hello";
String str2 = "hello";
if(str1 == str2)
System.out.println("1st time: str1=str2 (==Compare with) ");
else
System.out.println("1st time: str1 ≠ str2(==Compare with) ");
//Add the same string
str1 += "!";
str2 += "!";
if(str1 == str2)
System.out.println("Second time: str1=str2 (==Compare with) ");
else
System.out.println("Second time: str1 ≠ str2(==Compare with) ");
if(str1.equals(str2))
System.out.println("Third time: str1=str2 (Compare with equals) ");
else
System.out.println("Third time: str1 ≠ str2(Compare with equals) ");
}
Execution result
1st time: str1=str2 (==Compare with)
Second time: str1 ≠ str2(==Compare with)
Third time: str1=str2 (Compare with equals)
This code returns true in the first comparison using the ==
operator, but returns false in the second comparison after adding the string. The third comparison using the equals method returns true. In this way, when the ==
operator is used, false is returned if the object is different even if the referenced character string is the same, so use the equals method to compare the character strings.
Use equalsIgnoreCase if you want to compare as the same string without distinguishing between uppercase and lowercase letters.
public static void main(String[] args) {
String str1 = "hello";
String str2 = "HELLO";
if(str1.equalsIgnoreCase(str2))
System.out.println("str1=str2 ([Compare with equalsIgnoreCase) ");
else
System.out.println("str1≠str2 ([Compare with equalsIgnoreCase) ");
if(str1.equals(str2))
System.out.println("str1=str2 (Compare with equals) ");
else
System.out.println("str1≠str2 (Compare with equals) ");
}
Execution result
str1=str2 ([Compare with equalsIgnoreCase)
str1≠str2 (Compare with equals)
Use the equals method of the Objects class to make a safe comparison without raising a nullPointerException exception. In the equals method of the String class, a nullPointerException exception is thrown when the object that calls the method is null.
First, you need to import java.util.Objects
to use the equals method of the Objects class.
import java.util.Objects;
public class Main {
public static void main(String[] args) {
String str1 = null;
String str2 = "abc";
String str3 = null;
System.out.println(Objects.equals(str1, str2));
System.out.println(Objects.equals(str1, str3));
}
}
Execution result
false
true
When comparing null verbs in this way, true can be returned.
[Quick learning Java] Difference between ”==” and “equals” (explains how to deny) [Introduction to Java] Summary of how to compare with equals method
Recommended Posts