반응형 코딩/Java (20) 썸네일형 리스트형 자바 - 변수에 숫자 값 할당 시 가독성 높이는 법 (int a = 1,000,000 가능?) 자바에서 숫자(integer, float 등등) 타입의 변수에 값을 할당 시, 값의 길이가 길면 가독성이 떨어진다. public class Example { public static void main(String[] args) { int num = 100000000; // 얼마?? } } public class Example { public static void main(String[] args) { int num = 100,000,000; // error: expected } } 천 단위로 콤마를 넣으면 에러가 발생. public class Example { public static void main(String[] args) { int num = 100_000_000; System.out.printl.. Java - Switch 문 꿀팁 (중복 코드 간결화) 아래의 switch 문의 목표는 변수 day의 값이 "월요일", "수요일", "금요일" 중에 하나면 >>> "알바 가는 날" 출력. 변수 day의 값이 "화요일" 혹은 "목요일"이면 >>> "쉬는 날" 출력. public class Example { public static void main(String[] args) { String day = "월요일"; switch (day) { case "월요일": System.out.println("알바 가는 날"); break; case "화요일": System.out.println("쉬는 날"); break; case "수요일": System.out.println("알바 가는 날"); break; case "목요일": System.out.println("쉬는 .. 자바 - float 변수에 실수 값을 그냥 넣으면 에러 나는 이유 목차 문제 이유 해결책 문제 public class Main { public static void main(String[] args) { float number = 1.1; } } 데이터 타입이 float인 number라는 변수에 실숫값 1.1을 할당해 보자. example.java:3: error: incompatible types: possible lossy conversion from double to float float number = 1.1; ^ 1 error error: compilation failed shell returned number라는 변수의 데이터 타입이 실수(float)인데 실숫값이 할당이 안된다??? 이유 위에 에러 내용을 보면, double 형태에서 float 형태로 변환 .. 자바 - 문자열 속 따옴표 ("), 역슬래시 (\) 출력 방법 자바 문자열에 큰따옴표 (") 혹은 역슬래시 (\)를 넣으면 에러가 난다. public class Main { public static void main(String[] args) { System.out.println("hello world""); } } 문자열에 큰따옴표를 넣었다. example.java:3: error: unclosed string literal System.out.println("hello world""); ^ 1 error error: compilation failed shell returned 1 에러... println 함수 괄호 속 두 번째 따옴표가 hello world라는 문자열의 끝으로 인식한 건 괜찮다. 다만, 에러가 난 이유는 세 번째 따옴표가 또 다른 문자열의 시작으.. 이전 1 2 3 다음