main method
public static void main(String[] args){ }
: 메소드 선언%d : 정수, %f : 실수, %c : 문자, %s : 문자
package day01;
public class Test03_print {
public static void main(String[] args) {
System.out.print("Hello Great!\n");
System.out.println("\\"); // 역슬래시 출력
System.out.println("\"");
System.out.printf("%d\n", 10); // 정수 출력
System.out.printf("%4d\n", 10); // 왼쪽에 공백 4칸
System.out.printf("%-4d\n", 10); // 오른쪽에 공백 4칸
System.out.printf("%04d\n", 10); // 왼쪽에 0 4칸
System.out.printf("%f\n", 10.1); // 실수 출력
System.out.printf("%f\n", 10.12345678); // 실수는 소수점 6째까지
System.out.printf("%.2f\n", 10.12345678); // 소수점 둘째자리까지
// % [값의 순번][flasgs][width][.precision] conversion
// flags : 생략되면 왼쪽 공백, - 면 오른쪽 공백, 0이면 공백대신 0으로 채우기
}
}
/*
Hello Great!
\
"
10
10
10
0010
10.100000
10.123457
10.12
*/
int keyCode = System. + in. + read(); // 키코드를 하나씩 읽어서 문자열 못 읽음
Scanner scanner = new Scanner(System.in);
// scanner 변수선언 = 입력장치로부터 읽는 scanner 생성
String inputData = scanner.nextLine();
// string 변수선언 = 엔터키 이전까지 입력된 문자열 읽음
변수
변수 규칙
메모리 단위
자료형
//String -> int
String str = "300000";
int value = Integer.parseInt(str);
//String -> double
String str = "12.345";
double value = Double.parseDouble(str);
//문자열이 숫자 아닌 다른 타입 포함하면
// NumberFormatException 에러 발생
String str = "1a";
int value = Integer.parseInt(str);
//기본타입 값을 문자열로 변환
String str = String.valueOf(기본타입값); // "3"
변수 선언
int age; , String name;(참조형)
저장
age = 30; name = “철수”;
초기화
int age = 30;
형 변환
byte b = 100; int i = b;
int i = 100; byte b = i; (X) byte b = (byte) i; (O)