Notes on operators using Java ~ String type ~

1.First of all

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.

2. Precautions

2.1. == Operator

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.

2.2. + String concatenation

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!

3. Conclusion

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

Notes on operators using Java ~ String type ~
Try scraping using java [Notes]
Using Java on OSX 10.15 (Catalina) β
Notes on signal control in Java
Notes on Android (java) thread processing
[Java] Correct comparison of String type
Notes on Java path and Package
Java string
java notes
[Java] Express Enum type without using Enum (enumeration) type
Java date data type conversion (Date, Calendar, String)
Regarding String type equivalence comparison in Java
Using JupyterLab + Java with WSL on Windows 10
[Java] Data type / string class cheat sheet
Sobel filter using OpenCV on Android (Java)
[Java] Convert Object type null to String type
Notes on using FCM with Ruby on Rails
[Java] Comparison of String type character strings
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
[Java] Calculation mechanism, operators and type conversion
Type conversion from java BigDecimal type to String type
[Java] Display the bit string stored in the byte type variable on the console
Install java and android-sdk on Mac using homebrew
[JAVA] Stream type
[Java] String padding
Java Generics (Notes)
[Java] Enumeration type
Java Optional type
[Java] Array notes
Java double type
Java string processing
[Java] Study notes
Java serialization notes
Java type conversion (String, int, Date, Calendar, etc.)
Try communication using gRPC on Android + Java server
Split string (Java)
Upload and download notes in java on S3
Using Java 8 with Bluemix (on Liberty Runtime & DevOps Service)
Create a Java development environment using jenv on Mac
Install java and maven using brew on new mac
I tried using Log4j2 on a Java EE server
Try image classification using TensorFlow Lite on Android (JAVA)
Notes on how to use regular expressions in Java
Sorting using java comparator
Let's touch on Java
Notes on Protocol Buffers
Java formatted output [Notes]
Install Java on Mac
python notes on docker
[Android] Notes on xml
[Java] String comparison and && and ||
[Java] Data type ①-Basic type
Run PostgreSQL on Java
Java string multiple replacement
[Java] Control syntax notes
Using JDBC on Linux
Notes on multiple inheritance
Java NIO 2 review notes
Notes on regular expressions
Scraping practice using Java ②
[Java, Kotlin] Type Variance