Why Java String typeclass comparison (==) cannot be used

String class comparison

There are occasions when you use a comparison of equal values in if statements, etc. For example ...

【Q1】 A program that checks if the number you enter matches the answer

Q1.java


import java.util.Scanner;

class Q1 {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		System.out.print("Enter the number:");
		int n = stdIn.nextInt();

			if (n == 5) {
			System.out.print("Per");
			} else {
			System.out.print("Off");
			}
	}
}

In this case, it is a comparison of int type numbers. You can compare using ==. Now, let's look at the following case as a comparison of strings.

【Q2】 Mystery program

Q1.java


import java.util.Scanner;

class Q2 {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		System.out.print("What are the ingredients for tofu? :");
		String n = stdIn.next();

			if (judge.equals("soy")) {
			System.out.print("Per");
			} else {
			System.out.print("Off");
			}
	}
}

I'm using equals for comparison instead of ==. The reason for equals is ...

If you use == in a class type (reference type) comparison, it's not the content It will be a comparison of the "address" that contains the data.


Qiita用_equalsの比較1.PNG Qiita用_equalsの比較2.PNG Qiita用_equalsの比較3.PNG

Recommended Posts

Why Java String typeclass comparison (==) cannot be used
[Java] String comparison and && and ||
Because getSupportLoaderManager cannot be used
Java Stream cannot be reused.
[Java] Speed comparison of string concatenation
CentOS7 VirtualBOX yum cannot be used
[PHP] Solved "mb_strpos cannot be used"
Java cannot be installed on Ubuntu 13.04
[Java] Correct comparison of String type
Why Java Vector is not used
Three reasons why wrapper classes should not be used in Java
Note that system properties including JAXBContext cannot be used in Java11
Regarding String type equivalence comparison in Java
Java string
Java code that cannot be used from Kotlin (for in-house study sessions)
[Java] Comparison of String type character strings
[Java] Variables declared inside the `for statement` cannot be used outside the` for statement block`
The story of low-level string comparison in Java
[Rails 6] method :: delete cannot be used with link_to
[Java] Scenes that cannot be reassigned (substantially final)
[Solution] Java cannot be installed on Windows 10 + ATOK 2017
Kotlin's reified function cannot be called from java.
[Java] String padding
MyBatis string comparison
[Java] Map comparison
Java string processing
Java framework comparison
Split string (Java)
Please note that Spring Boot + Tomcat 8.5.8 cannot be used!
Tomcat cannot be started due to java version change
Systemctl cannot be used on Ubuntu inside Docker container
[Java] String join execution speed comparison (+ operator vs StringBuilder)
Scala String can be used other than java.lang.String method
[Java] When var should be used and when it should not be used
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests