[Java] Comparison of String type character strings

Nice to meet you. I've been learning Java for about 3 months and am a beginner! !! w

Currently, I am learning Java + HTML/CSS and JavaScript. I'm happy that my knowledge increases every day, and above all, I don't have time to research more than when I'm coding, so it's my favorite time. There are a lot of things that I have investigated from now on that overlap with other people, but please let me post it as a practice of output! !!

If you make a mistake, please point it out! !! Best regards: sunny:

So, this article is about comparing String type variables. In Java

if(Conditional expression){What you want to process}

You can compare with.

If it is an int type,

int a = 2;
int b = 2;
if(a == b ){
What you want to process
}

You can easily compare variables with. With the above content, the result is naturally true.

If you do this with a String type, ...

String a = "Corona Dokaike ";
String b = "Corona Dokaike ";
if( a == b ){
What you want to process
}

This has a true result.

Let's increase the number of entries ...

First of all, int type

int a = 2;
int b = 2;
int c = a + 5;
int d = b + 5;
if(c == d ){
 System.out.println("equal");
} else{
 System.out.println("Not equal");
}

This content is true because both values ​​are 7.

Then, in the main subject String type, ...

 String a = "Corona somewhere";
 String b = "Corona somewhere";
 String c = a + "!!!";
 String d = b + "!!!";
 if(c == d ){
  System.out.println("The contents are the same");
 } else{
  System.out.println("Content does not match");
 }

How about this content? ?? When I try to run it, I get a mismatch. If you look at the boxes of variables c and d humanly, you will think that they are the same because they are "Corona somehow !!!". Why is it the same "Corona Dokaike"? ?? ?? ?? It seems that the question arises for beginners. This is a common pitfall for beginners.

When comparing String types, consider whether they are equal or equivalent.

Equal value is "same value" Equivalence is "same content" is.

 String a = "Corona somewhere";
 String b = "Corona somewhere";
 String c = a + "!!!";
 String d = b + "!!!";
 if(c == d ){
  System.out.println("The contents are the same");
 } else{
  System.out.println("Content does not match");
 }

In, if you open the boxes of variables c and d, even if they have the same contents, they are not the same values.

The "==" used so far is for comparing the same values.

In other words, reference is to reserve an area in memory. In the above comparison, it is not the content but the comparison of areas (whether they are in the same place). It returns false.

Then, what should we do if we want to compare the contents even with the reference type like this? ?? This is where equivalence ("same content") comes into play.

For strings, equality comparisons use the equals () method. The usage is as follows.

String a = "Corona somewhere";
String b = "Corona somewhere";
String c = a + "!!!";
String d = b + "!!!";
if(c.equals(d)){
   System.out.println("The contents are the same");
} else{
   System.out.println("Content does not match");
}

You can use the equals () method in this way to compare reference strings. As you will learn from now on, even when comparing the same string of another instance with another instance etc., I think that you will not get a response that you think you will not use this equals () method. Remember to compare strings with the equals () method.

Recommended Posts

[Java] Comparison of String type character strings
[Java] Correct comparison of String type
[Java] Handling of character strings (String class and StringBuilder class)
[Java] Speed comparison of string concatenation
[Java] Comparison method of character strings and comparison method using regular expressions
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
Regarding String type equivalence comparison in Java
[Java] String comparison and && and ||
[java] Summary of how to handle character strings
Basics of character operation (java)
About full-width ⇔ half-width conversion of character strings in Java
[Java] How to convert a character string from String type to byte type
[Algorithm] Descending order of character strings
Java string
[Java] Divide a character string by a specified character
[Java] Implicit type cast (AOJ10-sum of numbers)
Java date data type conversion (Date, Calendar, String)
[Java] Data type / string class cheat sheet
Notes on operators using Java ~ String type ~
[Java] Convert Object type null to String type
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] Character judgment / character string formatting (AOJ11 --character count)
[Basic knowledge of Java] About type conversion
[Java] Precautions when comparing character strings with character strings
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] How to use substring to cut out a part of a character string
[Java] Check if the character string is composed only of blanks (= Blank)
Java type conversion
[Java] How to convert one element of a String type array to an Int type
[Java] Difference between equals and == in a character string that is a reference type
[JAVA] Stream type
[Delete the first letter of the character string] Ruby
Manipulating Java strings
[Java] String padding
Character string comparison: I was caught in the skill check problem of Paiza
MyBatis string comparison
[Java] Enumeration type
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
[Java] Be careful of the key type of Map
[Java] How to easily get the longest character string of ArrayList using stream
Calculate the similarity score of strings with JAVA
Java double type
[Java] Map comparison
Java string processing
Java type conversion (String, int, Date, Calendar, etc.)
Java character code
Split string (Java)
Why Java String typeclass comparison (==) cannot be used
[Java] Overview of Java
Java review ③ (Basic usage of arrays / reference type)
[Java] Incompatible types error occurred when assigning a character string to a char type variable
[Java] Cut out a part of the character string with Matcher and regular expression
[Java] Meaning of Public static void main (String [] args)
The comparison of enums is ==, and equals is good [Java]
[Java] String join execution speed comparison (+ operator vs StringBuilder)
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
[java.io] Summary of Java string input (InputStream, Reader, Scanner)