[Java] Precautions when comparing character strings with character strings

Conclusion: Use equals () when comparing strings stored in variables in Java.

What you want to do

reference

Comparison of strings to strings https://www.javadrive.jp/start/string/index4.html

[Introduction to Java] How to compare strings ("==" and "equals") https://www.sejuku.net/blog/14621

When substituting a numerical value for a variable and comparing

int num = 10

if (num == 10) {
 System.out.println("num is 10.");
} else {
 System.out.println("num is not 10.");
}

In this formula, 10 is stored in the variable num, so the processing result is

num is 10.

When substituting a string type for a variable and comparing

String str1 = "hoge"
String str2 = "hoge"

if (str1 == str2) {
 System.out.println("str1 and str2 are the same.");
} else {
 System.out.println("str1 and str2 are different.");
}

In this formula, the variable str1 and the variable str2 store the same character string hoge, so I thought that the processing result would be "str1 and str2 are the same."

str1 and str2 are different.

.. .. .. !? Hoge !? Why !? I got stuck in a pot here and debugged about 300 times.

Method "==" to compare if the location where the strings are stored in memory is the same

Even if you compare with "==", it will be treated as a different character string. This is called a Java reference

When the character string is generated, a place (area) for storing the character string is secured in the memory. Hoge holds the reserved "place"

In other words, the value is not stored in a variable, but the mechanism is such that when you remember the location and use hoge, you follow that location and pull out the value.

Since there is a mechanism called this reference, when comparing with "==", it is not whether the values of the strings are the same, but whether the locations where the strings are stored in the memory are the same.

Returns true if stored in the same memory area, otherwise false In the previous formula, str1 and str2 are stored in different memory areas, so they return false.

Compare strings using the equals () method

String str1 = "hoge"
String str2 = "hoge"

if (str1.equals(str2)) {
 System.out.println("str1 and str2 are the same.");
} else {
 System.out.println("str1 and str2 are different.");
}

Processing result

str1 and str2 are the same.

The expected result is returned.

By the way, if you write in ruby, the same result will be returned even with "=="

str1 = "hoge"
str2 = "hoge"

if str1 == str2  then
 puts "str1 and str2 are the same."
else
 puts "str1 and str2 are different."
end

# =>str1 and str2 are the same.

Summary

When using ruby or JavaScript, both numeric type and string type can be compared using "==", so I wrote the program in Java without a doubt, but Java is different.

I checked if there were enough grammatical mistakes to make holes in the screen, but be careful because if you do not know "reference", it will take extra time for me.

end

Recommended Posts

[Java] Precautions when comparing character strings with character strings
Sort strings functionally with java
[Rails] Precautions when comparing date and time with DateTime
Error when playing with java
Precautions when replacing backticks with gsub
[Java] Remove whitespace from character strings
Precautions when creating PostgreSQL with docker-compose
Memo when HTTP communication with Java (OkHttp)
When calling API with java, javax.net.ssl.SSLHandshakeException occurs
[Java] Comparison of String type character strings
Precautions when migrating from VB6.0 to JAVA
[java] Summary of how to handle character strings
Manipulating Java strings
Specify ClassPath when using jupyter + Java with WSL
Calculate the similarity score of strings with JAVA
Java character code
[Java] Consideration when handling negative binary numbers with Integer.parseInt ()
[Java] Sorting tips when strings and numbers are mixed
Notice multi thread problem when working with Java Servlet
About full-width ⇔ half-width conversion of character strings in Java
[Java] Handling of character strings (String class and StringBuilder class)
Install java with Homebrew
Change seats with java
Install Java with Ansible
Comfortable download with JAVA
Switch java with direnv
Download Java with Ansible
Let's scrape with Java! !!
Build Java with Wercker
Endian conversion with JAVA
[Java] Comparison method of character strings and comparison method using regular expressions
Specify the character code of the source when building with Maven
Initial setting method when making Alexa Skill with JAVA (Cloud9)
About the behavior when doing a file map with java
Precautions when using Mockito.anyString as an argument when Mocking with Mockito
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
[Java] UTF-8 (with BOM) is converted to 0xFFFD (REPLACEMENT CHARACTER)
FactoryBot, a solution when played with a foreign key validation
Precautions when writing a program: Part 3
Precautions when replacing backticks with gsub
Precautions when creating PostgreSQL with docker-compose
[Java] Precautions when comparing character strings with character strings