This time, I will talk about operators using the String type. Since there are some points that need attention, I will divide the articles.
Some operators may not be used as-is for the String type. Rather, it's okay to think of it as something completely different.
If you try to use this == operator with a String type, you probably won't get what you want.
Main.java
public class Main {
public static void main(String[] args) {
String first = "num";
String second = "num";
first += "ber";
second += "ber";
//Show first string
System.out.println(first); //number and output
//Display the second string
System.out.println(second); //Same as first number and output
//first"number"And second"number"Compare
System.out.println(first == second); // false
}
}
This time, for convenience, both first and second are initialized with "num".
Then I add "bar" to both first and second. You can add a string to the String type with + =.
Both the first and second lines should be "number"!
I think the output on the third line was false. False means that first and second are not the same thing.
So what if you want to make sure that the strings of type String are the same?
If you want to compare strings of type String, use something called String.equals (). Put a new source that replaces the first == second part above.
Main.java
public class Main {
public static void main(String[] args) {
String first = "num";
String second = "num";
first += "ber";
second += "ber";
//Show first string
System.out.println(first); //number and output
//Display the second string
System.out.println(second); //Same as first number and output
//first"number"And second"number"Compare
System.out.println(first.equals(second)); // true
}
}
The third line this time was displayed as true! It may be a little confusing, but use String.equals () to compare strings.
Next is the problem with string concatenation. If you want to concatenate strings and numbers, you can concatenate them with +.
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("1 + 2 = " + 3);
}
}
Like this ... You should see 1 + 2 = 3.
And a common mistake is when the source is as follows.
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("1 + 2 = " + 1 + 2);
}
}
When you run it, you should see 1 + 2 = 12.
At this time, the operation starts from the left, and "" 1 + 2 = "+ 1" is performed first. The result of this operation is "" 1 + 2 = 1 "", which is a String type.
Next, the operation is "" 1 + 2 = 1 "+ 2", and "" 1 + 2 = 12 "" is the result, which is also of type String.
At this time, if you want to display 1 + 2 = 3 normally, you can solve it by changing the priority of the operation. Use (), which was also used in math.
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("1 + 2 = " + (1 + 2));
}
}
If 1 + 2 = 3 is displayed, it's perfect!
Here are just two things to keep in mind when using Strings and operators.
By the way, try using other operators. There should be an operator that causes a compile error.
This time is over!
Recommended Posts