Studying Java ―― 8

Compare strings with if conditional expressions

The "==" used for numerical values cannot be used for character string comparison. For character strings, use "** equals **".

The way of writing is a little different from when "=="

Variable a.equals(Variable b)

It seems to write. When you want to compare variables s1 and s2 with if, use it like this ↓

String s1 = "tomato", s2 = "tomato";

if (s1.equals(s2)){
	System.out.println("match");
}

This ↑ is executed in if when s1 and s2 match If you want to "when the strings do not match" Enclose the conditional expression in front of and behind it with "()" and add "!" In front of it.

!(Variable a.equals(Variable b))

When the variables s1 and s2 are different, it is ↓ to execute in if

String s1 = "tomato", s2 = "potato";

if (!(s1.equals(s2))){
	System.out.println("Mismatch");
}

Feeling accustomed to using "!" For denial.

Assigned to a variable in if and an error occurred

The first time to put a value in a variable is in if, An error occurred when trying to use the value of that variable in subsequent processing.

You may not know what you mean Is the person who writes "Hmm?" I don't want you to worry about it.

The error occurred when ↓

Yomogi.java


public class Yomogi{
	public static void main(String[] args){
		int a, b;
		a = 1;
		
		if (a == 1){
			b = 1;  //With this
		}	
		System.out.println("b = " + b);  //This relationship is useless
	}
}

Since the variable a contains 1, if becomes 100% true This is fine because the internal processing of if is executed. I thought, but it didn't work.

It seems that the problem was that the value was entered for the first time in the if. Therefore, enter an appropriate value in the variable b in advance.

Yomogi.java


public class Yomogi{
	public static void main(String[] args){
		int a, b;
		a = 1;
		b = 0;  //Initialized with 0 in advance
		
		if (a == 1){
			b = 1;
		}
		System.out.println("b = " + b);
	}
}

It worked well in this way. I see, java has that kind of specification.

I understood.

Even if the first assignment to a variable is in an if It was OK to use that variable only in if, so I decided to remember it.

Yomogi.java


public class Yomogi{
	public static void main(String[] args){
		int a, b;
		a = 1;
		
		if (a == 1){
			b = 1;
			System.out.println("b = " + b);  //It's OK to use it only in if
		}
	}
}

When using if, you have to be careful when assigning to variables. It was an encounter with an error that I thought.

Recommended Posts

Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying Java 8 (Optional)
Studying java9 (jShell)
Studying Java 8 (Stream)
Studying Java 8 (Collector / Collectors)
Studying Java 8 (see method)
Java
Studying Java ~ Part 8 ~ Cast
Studying Java 8 (lambda expression)
Java
Java learning (0)
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
java (override)
java (method)
Java Day 2018
Java string
Studying Java # 6 (How to write blocks)
Java static
Java serialization
java beginner 4
JAVA paid
Java (set)
[Java] compareTo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
[Java] Array
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
Java inheritance
[Java] Overload
Java basics
[Java] Annotation