[Java] String comparison and && and ||

String comparison

Example.java


number == 0

Suppose you have code like the one above. The meaning is that 0 is assigned to the variable number, which is equal to 0 on the right.

Then, in the case of letters ...? : thinking:

Example.java


fruit == "Apple"

Isn't it? ?? : thinking: I think. In fact, it's a mistake. In Java, strings are supposed to be incomparable with ==. So what to do ...

String comparison.


String type variable.equals(String of comparison partner)

Will be: smile: So the previous code is

Example.java


fruit.equals("Apple")

Will be: smile:

As an aside, using == in a string doesn't cause a compile error: sweat_smile: There is a bad problem that it can be executed but the movement is strange: sweat_smile: It's awkward to find an error just because it doesn't cause a compile error: sweat_smile:

&&When||

: sunny: && is used when you want to match all two or more conditions such as "female and born in Tokyo". :sunny: ||Is used when you want to match something that meets any of the conditions, such as "female or born in Tokyo".

(Example) Over 20 years old and born in May

Example.java


age >= 20 && month == 5

True if you are over 20 years old and born in May and all the conditions are met, otherwise false.

(Example) Over 20 years old or born in May

Example.java


age >= 20 || month == 5

False if you are 20 years old or older or born in May and none of the above apply, otherwise true.

Recommended Posts

[Java] String comparison and && and ||
Java string
[Java] About String and StringBuilder
Studying Java 8 (String Joiner and join)
[Java] Correct comparison of String type
[Java] String padding
MyBatis string comparison
[Java] Map comparison
Java string processing
Java framework comparison
Java and JavaScript
XXE and Java
Split string (Java)
Regarding String type equivalence comparison in Java
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] Collection and StringBuilder operation method comparison
[Java] Comparison of String type character strings
The story of low-level string comparison in Java
Getters and setters (Java)
[Java] Thread and Runnable
String and stringbuffer and string builder
Java version notation comparison
String literals and instances
[Java] The confusing part of String and StringBuilder
Java string multiple replacement
Comparison operators and conditionals
Java and Swift comparison (1) Source control / Scope / Variables
[Note] Java: String search
[Note] Java: String survey
About Java String class
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
[Development] Java framework comparison
[Java] Variables and types
Why Java String typeclass comparison (==) cannot be used
java (classes and instances)
[Java] Overload and override
<java> Read Zip file and convert directly to string
The comparison of enums is ==, and equals is good [Java]
[Java] String join execution speed comparison (+ operator vs StringBuilder)
[Java] About Objects.equals () and Review of String comparisons (== and equals)
Equivalence comparison of Java wrapper classes and primitive types
[Java] Handling of character strings (String class and StringBuilder class)
Java version 8 and later features
[Java] Stack area and static area
Java inflexible String class substring
java ArrayList, Vector, LinkedList comparison
Reflection on Java string manipulation
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
Java if and switch statements
[Java] Type conversion speed comparison
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
String concatenation and line breaks
[Java] HashCode and equals overrides