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

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

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

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
반응형

+ Recent posts