이원자 탄소 2022. 1. 19. 16:21
728x90

Data types in JAVA

이걸 다시 복습해보자. 일단 int, double, char을 제일 많이쓸거니까 heighlight 쳐놓을게

type description range
boolean true or flase true, false
byte twos-complement integer -128 to 127
char  Unicode character ASCII value 0 to 255
short twos-complement integer  -32768 to 32767
int twos-complement integer  -2147483648 to 2147483647
long twos-complement integer  -9223372036854755808 to 922337203685475587
float IEEE 754 floating point up to 7 decimal digits
double IEEE 754 floating point up to 16 decimal digits

 

Integer overflow 

 

Number conversion

class Main {
    public static void main(String[] args) {
        int big;
        byte small;

        big = 10;
        small = big;

        System.out.println(big);
        System.out.println(small);
    }
}

이렇게 하면 compile error 남. 왜? big(int) 에 들어가있는 value가 small(byte)에 들어가려고하면 에러나지. C면 돌아가기라도 할텐데 자바는 바로 에러난다는걸 보여주는 예시. 

 

+number conversion 예시:

 

3.75를 integer 로 바꾸면 3이 나온다. 이걸 다시 floatfh 바꾸면 3.0이 나온다. 즉 3.75를 int에 넣으면 3이되는거임. 0.75 순삭됨



Character type: Char

char c;

c='a';

Single quotation mark 으로 c 에 character를 넣으면 됨. 

ASCII table 에서 character 0랑 integer 0 는 다르다. 

보셈 character 0은 48에 있음. 

 

Relational Operators

>,< 이런걸 뜻한다. 

근데 여기 룰이 있는데,

Rule #1. The two inputs must be numbers, not necessarily of the same type.

Rule #2. If the two inputs are numbers with different types, the inferior one will be upgraded automatically.

 

relational operator를 사용한 코드:


class Main {
    public static void main(String[] args) {
        double UV_level = 12;
        boolean UV_danger;

        UV_danger = (UV_level > 10);

        // warn the user
        System.out.println("UV Danger: " + UV_danger);
    }
}

자 이 코드를 분석하자면, UV_danger라는 이름을 가진 boolean 변수는 0또는 1이다. boolean 이니까.

그니까 UV_danger는 UV_level이 10보다 클시에 true 인 1, 크지 않을시에 false 인 0이 되는거다. 

지금같은 케이스는 UV_level에 12이고, 10보다 크니까 true 인 1이 UV_danger에 들어갈것이당

 

이러한 operator가 있다. 

== 랑 =는 다르다. =는 assignment operator고 ==는 equality operator다. 

쉽게말하면 =는 걍 값 넣는거고 ==는 같다는뜻.

 

Boolean (or logical) operator

걍 true 냐 false냐 따지는 operator 말하는거다. 

 

 이처럼 !나 &&따위의 operator 를 말한다. 

 

&&(and라는뜻) 는 걍 

condition a && condition b 가 있다면, 

a랑 b 둘다 true 일 시에 true가 나오는거고

두게중 하나라도 false거나 둘다 false면 걍 가차없이 false 나오는거지. 

마치 하나라도 틀리면 0점주는 문제처럼

 

||(or라는뜻) 은 뭐냐면

condition a || condition b 이렇게 있으면 

둘다 false 가 아닌이상 다 true로 나온다. or이니까 둘중에 하나라도 true가 있으면 true인거지. 

 

Operator Precedence & Associativity

자 이런식으로! 뭐가 더 중요하냐 이거임. 지금 저기에선 10>=4+10&&4+6==10이 있으면, (10>=4+10)랑 (4+6==10)를 비교해서 둘다 true이냐를 따지는건데, 일단 첫번째 condition 인 10>=4+10 부터가 false니까 가차없이 false 나오는거지.

 

그래서 결괴는 false. 

 

728x90