Not 을 표현하기 위한 기호다
!
매우 직관적이기 때문에 수업도 수월하게 이햇했다 .
public class App {
public static void main(String[] args) {
// ! "not operator" unary operator
// == "equality test operator" binary operator
// + "plus" binary operator
// - "minus" binary operator etc.
System.out.println(4 == 5);
System.out.println(4 == 5);
// you can only use ! on boolean value
// System.out.println(!4 == 5);
System.out.println(!(4==5));
int value1 = 4;
int value2 = 3;
if(!(value1 == value2)) {
System.out.println("values are not equal.");
}
}
}
괄호를 쳐서, 괄호안에 있는 statement 자체를 부정하는것이 편리하다.

기본적으로 ! 은 boolean 에서만 쓸 수 있다.
package application;
public class App {
public static void main(String[] args) {
// != "not equals" binary operator
int value1 = 4;
int value2 = 3;
// if (!(value1 == value2)) {
if (value1 != value2) {
System.out.println("Value are not equal");
}
System.out.println(value1 != value2);
System.out.println(value1 == value2);
String fruit1 = "orange";
String fruit2 = "apple";
if (!fruit1.equals(fruit2)) {
System.out.println("Fruits are not the same");
}
// Unnecessarily confusing, but instructive... ㅎ
if (fruit1.equals(fruit2) != true) {
System.out.println("Fruits are not the same");
}
System.out.println(fruit1.equals(fruit2));
System.out.println(fruit1.equals(fruit2) == true);
System.out.println(fruit1.equals(fruit2) == false);
System.out.println(fruit1.equals(fruit2) != true);
System.out.println(fruit1.equals(fruit2) != false);
}
}
다소 복잡해보이기는 하지만...
우선 쉽고..
이렇게 복잡하게 표현해야 되는 상황이 또 발생할지도?

해피코딩
'콤퓨타 > Programming) 왕초보 자바' 카테고리의 다른 글
| 27. (Loops and Conditions) Checking Password with Do-While (feat. 챗지피티 활용 위기관리) (6) | 2025.08.17 |
|---|---|
| 27. (Loops and Conditions) Variable Scope (6) | 2025.08.14 |
| (번외) do while 연습 (feat. 증감연산자) (1) | 2025.08.11 |
| 25. (Loops and Conditions) Do While Loop (1) | 2025.08.11 |
| 24. (Loops and Conditions) Checking Passwords (실패코드 포함) (7) | 2025.08.11 |