목록이것이 자바다 (108)
충분히 쌓여가는
- 피연산자가 정수 타입(byte, short, char, int)이면 연산의 결과는 Int 타입 - 피연산자가 정수 타입이고 그 중 하나가 long 타입이면 연산의 결과는 long 타입 - 피연산자 중 하나가 실수 타입이면 연산의 결과는 실수 타입 package ch03.sec02; public class ArithmeticOperatorExample { public static void main(String[] args) { byte v1 = 10; byte v2 = 4; int v3 = 5; long v4 = 10L; int result1 = v1+v2; // 모든 피연산자는 int 타입으로 자동 변환 후 연산 System.out.println("result1: " + result1); long r..
부호 연산자는 변수의 부호를 유지하거나 변경한다: +, - 주의사항 byte b = 100; byte result = -b; // 컴파일 에러 정수 타입(byte, short, int) 연산식의 결과는 int 타입이다 부호를 변경하는 것도 연산이므로 int 타입 변수에 대입해야 한다 byte b = 100; int result = -b;
문자열 -> 숫자 타입 Byte.parseByte(); Short.parseShrot(); Integer.parseInteger(); Long.parseLong(); Float.parseFloat(); Double.parseDouble(); Boolean.parseBoolean(); 숫자 타입 -> 문자열 String str = String.valueOf(기본타입값); package ch02.sec10; public class PrimitiveAndStringConversionExample { public static void main(String[] args) { int value1 = Integer.parseInt("10"); double value2 = Double.parseDouble("3.14"..
자바에서 + 연산자는 두 가지 기능을 가지고 있다 피연산자가 모두 숫자일 경우에는 덧셈 연산을 수행 피연산자 중 하나가 문자열일 경우에는 나머지 피연산자도 문자열로 자동 변환되어 문자열 결합 연산을 수행 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 +..