> For the complete documentation index, see [llms.txt](https://leeans-dev-book.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://leeans-dev-book.gitbook.io/docs/lecture/java/section2./undefined.md).

# 정수자료형과 연산자

## 정수 자료형과 연산자, 형변환

```java
package sec03.chap01;

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

    byte _1b_byte = 1;
    short _2b_short = 2;
    int _4b_int = 3; // ⭐️ 일반적으로 널리 사용
    long _8b_long = 4;

    //  ⚠️ 자료형의 범주 외의 수를 담을 수 없음
    byte overByte1 = 127;
    byte overByte2 = 128;
    byte overByte3 = -128;
    byte overByte4 = -129



    //  큰 자료형에 작은 자료형의 값을 넣을 수 있음
    //  💡 묵시적(암시적) 형변환
    _2b_short = _1b_byte;
    _4b_int = _1b_byte;
    _4b_int = _2b_short;
    _8b_long = _1b_byte;
    _8b_long = _2b_short;
    _8b_long = _4b_int;

    //  ⚠️ 작은 자료형에 큰 자료형의 값을 '그냥' 넣을 수 없음
    //  들어있는 값의 크기와 무관
    _1b_byte = _2b_short;
    _1b_byte = _4b_int;
    _1b_byte = _8b_long;
    _2b_short = _4b_int;
    _2b_short = _8b_long;
    _4b_int = _8b_long;
    }
    
    //  ⭐ int의 범위를 벗어나는 수에는 리터럴에도 명시 필요
    //  끝에 l 또는 L을 붙여 볼 것
    long _8b_long1 = 123456789123456789L;

    
    // 💡 가독성을 위해 아래와 같이 표현 가능 (자바7부터)
    int _4b_int2 = 123_456_789;
    long _8b_long2 = 123_456_789_123_456_789L;
}
```

```java
package sec03.chap01;

public class Ex02 {
    public static void main(String[] args) {
        byte byteNum;
        int smallIntNum = 123;

        //  명시적(강제) 형변환
        //  - 개발자 : "내가 책임질테니까 그냥 넣어줘."
        byteNum = (byte) smallIntNum;
        
        int intNum = 12345;

        //  ⚠️ 강제로 범주 외의 값을 넣을 경우 값 손실
        byteNum = (byte) intNum; // 💡 12345 % 128
    }
}

```

## 이항 연산자

```java
    int a = 1 + 2;
    int b = a - 1;
    int c = b * a;
    int d = a + b * c / 3;
    int e = (a + b) * c / 3;
    int f = e % 4;
    
    
    
    byte b1 = 1;
    byte b2 = 2;
    short s1 = 1;
    short s2 = 2;
    
    //  ⚠️ 아래는 모두 불가
    byte b3 = b1 + b2;
    short s3 = b1 + b2;
    short s4 = b1 + s2;
    short s5 = s1 + s2;
    
    //  ⭐ byte와 short의 연산들은 int 반환
    //  int를 많이 쓰는 이유 중 하나
    int i1 = b1 + b2;
    int i2 = s1 + s2;
    int i3 = b1 + s1;
    
    long l1 = 1;
    long l2 = 2;

    // long끼리의 연산은 long 반환
    int l3 = l1 + l2;
    
    //  ⚠️ 정수 자료형의 계산은 소수점 아래를 '버림'
    byte int1 = 5/2;
    int int2 = 10;
    int int3 = 3;
    int int4 = int2 / int3;
```

## 복합대입 연산자

<table><thead><tr><th width="165.5">a += b</th><th>a = a + b</th></tr></thead><tbody><tr><td>a -= b</td><td>a = a - b</td></tr><tr><td>a *= b</td><td>a = a * b</td></tr><tr><td>a /= b</td><td>a = a / b</td></tr><tr><td>a %= b</td><td>a = a % b</td></tr></tbody></table>

```java
package sec03.chap01;

public class Ex04 {

    public static void main(String[] args) {
        int a = 1;
        a = a + 2;

        a += 3; // 🔴

        // 값을 반환하기도 함
        int b = a += 4; // 🔴
        
        
        int x = 3;
        x += 2;
        x -= 3; // 🔴
        x *= 12;
        x /= 3;
        x %= 5;
    }
}

```

```java
package sec03.chap01;

public class Ex05 {
    public static void main(String[] args) {
        // 💡 자료형의 범위를 넘어가도록 숫자를 더하거나 뺄 경우
        byte x = 127;
        x += 1;

        byte y = -128;
        y -= 1;

    }
}

```

| 연산자  | 반환값     | 부수효과 |
| ---- | ------- | ---- |
| +    | 값 그대로   | 없음   |
| -    | 양음 반전   | 없음   |
| x ++ | 값 그대로   | 1 증가 |
| ++x  | 1 증가한 값 | 1 증가 |
| x--  | 값 그대로   | 1 감소 |
| --x  | 1 감소한 값 | 1 감소 |

```java
	int int1 = 3;

        int int2 = int1++; // 🔴
        int int3 = ++int1;
        int int4 = -(int2-- * --int3);
        
	int x = 1;
        
        //  메서드 안으로도 '반환'되어 사용되는 것
        System.out.println(x++);
        System.out.println(++x);
        System.out.println(x);
        
        
        //  ⚠️ 리터럴에는 사용 불가
        int int5 = 3++;
        int int6 = --3;
```

## 비교연산자

| a == b | a와 b는 같다      |
| ------ | ------------- |
| a != b | a와 b는 다르다     |
| a > b  | a가 b 보다 크다    |
| a >= b | a가 b보다 크거나 같다 |
| a < b  | a가 b보다 작다     |
| a <= b | a가 b보다 작거나 같다 |

* `boolean` 자료형의 값을 반환 (`true` 또는 `false`)
* `=` \*( 대입 연산자 )\*와 혼동하지 말 것

```java
package sec03.chap01;

public class Ex07 {
    public static void main(String[] args) {
        //  값을 바꿔가면서 실행해 볼 것
        int int1 = 3;
        int int2 = 3;

        //  다른 정수 자료형끼리 사용 가능
        boolean bool1 = int1 == int2;
        boolean bool2 = int1 != int2;

        boolean bool3 = int1 > int2;
        boolean bool4 = int1 >= int2;

        boolean bool5 = int1 < int2;
        boolean bool6 = int1 <= int2;

        //  💡 우선순위에 따른 연산
        boolean bool7 = int1 * int2 > int1 + int2;
    }
}

```

<figure><img src="/files/cKbgFrCJh9QpBSHQeXh3" alt=""><figcaption><p>연산자 우선순위</p></figcaption></figure>

## "==" 과 equlas()의 차이

```java
package sec03.chap01;

public class EqualsTest{
    public static void main(String[] args){
        String a = "JAVA";
        String b = "JAVA";
        String c = new String("JAVA");
        String d = new String("JAVA");

        System.out.println( a == b );  // true
        System.out.println( b == c );  // false
        System.out.println( c == d );  // false

        System.out.println( a.equals(b) );  // true
        System.out.println( b.equals(c) );  // true
        System.out.println( c.equals(d) );  // true
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://leeans-dev-book.gitbook.io/docs/lecture/java/section2./undefined.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
