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
    }
}