Notice
Recent Posts
Recent Comments
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

충분히 쌓여가는

2.9 연산식에서 자동 타입 변환 - +의 두 가지 기능 본문

이것이 자바다/02 변수와 타입

2.9 연산식에서 자동 타입 변환 - +의 두 가지 기능

빌드이너프 2024. 3. 28. 18:41

자바에서 + 연산자는 두 가지 기능을 가지고 있다

피연산자가 모두 숫자일 경우에는 덧셈 연산을 수행

피연산자 중 하나가 문자열일 경우에는 나머지 피연산자도 문자열로 자동 변환되어 문자열 결합 연산을 수행

package ch02.sec09;

public class StringConcatExample {
	public static void main(String[] args) {
		int result1 = 10 + 2 + 8;
		System.out.println(result1);
		
		String result2 = 10 + 2 + "8";
		System.out.println(result2);
		
		result2 = 10 + "2" + 8;
		System.out.println(result2);
		
		result2 = "10" + 2 + 8;
		System.out.println(result2);
		
		result2 = "10" + (2 + 8);
		System.out.println(result2);
	}
}
20
128
1028
1028
1010