Spring/JAVA

Java 제어문1 - Boolean, 비교&논리 연산자

alsruds 2023. 10. 4. 04:35
🐣생활코딩 강의🐣

JAVA 제어문
https://www.youtube.com/playlist?list=PLuHgQVnccGMCoEXnWV8-UF1mBxK5ftmxH

 

Boolean

· true

· false

public class Main {
    public static void main(String[] args) {

        String foo = "Hello World";
        // String true = "Hello"; reserved word

        // contains : String 비교로 true/false 반환
        System.out.println(foo.contains("World")); // true
        System.out.println(foo.contains("Hi")); // false
    }
}

 

비교 연산자 : Comparison Operator

public class Main {
    public static void main(String[] args) {
        System.out.println(1 > 1); // false
        System.out.println(1 < 1); // false
        System.out.println(1 == 1); // true
        System.out.println(1 >= 1); // true
    }
}

 

☑️ == vs. equals

· == : 원시 데이터 타입(primitive) 비교에 사용 → boolean, int, double, short, long, float, char

· equals : non primitive 비교에 사용 string, array, date, file ...

강의 내 이미지

 

논리 연산자 : Logical Operator

· AND : 둘 다 만족해야 true

· OR : 하나만 만족해도 true

· NOT : 반대

public class Main {
    public static void main(String[] args) {
        // AND
        System.out.println(true && true); // true
        System.out.println(true && false); // false
        System.out.println(false && true); // false
        System.out.println(false && false); // false

        // OR
        System.out.println(true || true); // true
        System.out.println(true || false); // true
        System.out.println(false || true); // true
        System.out.println(false || false); // false

        // NOT
        System.out.println(!true); // false
        System.out.println(!false); // true
    }
}