# 증감 연산자와 부호 연산자

## 증감 연산자

> ### 증가 연산자(++) : 피연산자의 값을 1 증가 시킵니다.
>
> ### 감소 연산자(--) : 피연산자의 값을 1 감소 시킵니다.

|  타입 |                           설명                           |             사용 예            |
| :-: | :----------------------------------------------------: | :-------------------------: |
| 전위형 | 값이 참조되기 <mark style="color:blue;">전에</mark> 증가/감소 시킨다. | <p>k = ++j;<br>k = --j;</p> |
| 후위형 |  값이 참조된 <mark style="color:blue;">후에</mark> 증가/감소 시킨다. | <p>k = j++;<br>k = j--;</p> |

> ### 증감 연산자가 독립적으로 사용된 경우, 전위형과 후위형의 차이는 없습니다.
>
> * ++k;  k++; = > 값 동일

* 아래 예제코드로 학습해 보겠습니다.

{% code lineNumbers="true" %}

```java
class Operator2_1 {
    public static void main(String[] args) {
        int k = 0, j = 3;

        // 후위 형
        k = j++;
        System.out.println("k = j++;, k = " + k);
        System.out.println("k = j++;, j = " + j);
        // 다르게 표현 가능
//        k = j;
//        j++;
//        System.out.println("k = j++;, k = " + k);
//        System.out.println("k = j++;, j = " + j);

        // 값 초기화
        k = 0;
        j = 3;

        // 전위 형
        k = ++j;
        System.out.println("k = ++j;, k = " + k);
        System.out.println("k = ++j;, j = " + j);
        // 다르게 표현 가능
//        ++j;
//        k = j;
//        System.out.println("k = ++j;, k = " + k);
//        System.out.println("k = ++j;, j = " + j);
        
    }
}
```

{% endcode %}

## 부호 연산자

> ### '-' 는 피연산자의 부호를 반대로 변경합니다.
>
> ### '+' 는 아무런 일도 하지 않습니다.(실제 사용X)

* 아래 예제코드로 학습해 보겠습니다.

{% code lineNumbers="true" %}

```java
class Operator2_2 {
    public static void main(String[] args) {
        int i = -10;
        i = +i;
        System.out.println("i = +i; = " + i);

        i = -10;
        i = -i;
        System.out.println("i = -i; = " + i);
    }
}
```

{% endcode %}

{% hint style="info" %}
Ref. Java의 정석 기초편 Chapter3(5, 6)

Ref. [증감, 부호 연산자](https://www.youtube.com/watch?v=d2uDDwz2fuc\&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp\&index=25)
{% endhint %}


---

# Agent Instructions: 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://nbcamp.gitbook.io/java-handbook/part-02./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.
