JAVA 프로그래밍
[JAVA] 풀스택 개발자 부트캠프 002일차 ① 변수 대입과 교환
selfdailycoding
2024. 12. 25. 14:57
1. 변수의 대입
오른쪽의 값을 왼쪽에 대입함.
public class MainClass {
public static void main(String[] args) {
int a, b;
a = 11;
b = a; // 대입
System.out.println("b = " + b);
}
}

2. 교환(swap)
- 두 값을 서로 교환할 수 있음.
- 첫 번째 값을 임시로 저장할 수 있는 별도 변수가 필요함. (아래 예시에서는 temp)
public class MainClass {
public static void main(String[] args) {
int numberOne = 11;
int numberTwo = 22;
int temp; // 임시 저장 변수 = 보관소
temp = numberOne;
numberOne = numberTwo;
numberTwo = temp;
System.out.println("numberOne = " + numberOne);
System.out.println("numberTwo = " + numberTwo);
}
}
