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