변수와 자료형 - 숫자, 부울, 문자
1. 변수(Variable)
- 데이터를 저장하는 메모리 공간에 붙여준 이름
1
2
int age = 20;
String country = "Korea";
2. 변수 이름 규칙
1) 문자와 숫자, 언더바(_), $ 사용가능
2) 숫자로 시작 X
3) 대문자와 소문자 구분
4) 공백 사용 X
5) 미리 예약된 이름 사용 X
3. 표기법
- 카멜 표기법(camelCase)
- 가장 앞의 문자는 소문자로, 나머지 단어의 첫 문자는 대문자로 표기
ex) myName, zeroBase, iPhone, powerPoint
- 파스칼 표기법(PascalCase)
- 각 문자의 첫문자를 대문자로 표기
ex) MyName, ZeroBase, IPhone, PowerPoint
- 자료형
- 변수의 종류, 단위
- 숫자 (Number)
- 부울 (Boolean)
- 문자 (Character)
- 문자열 (String) - 변수의 종류에 따라 담을 수 있는 데이터의 타입과 크기가 다름
4. 자료형 - 숫자(Number)
ex) 정수형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Java 프로그래밍 - 변수와 자료형
public class Main {
public static void main(String[] args) {
// 자료형 - 숫자
System.out.println("== 숫자 ==");
// 1-1. 정수
int intNum = 10;
System.out.println("intNum = " + intNum);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
int intNum2 = Integer.MAX_VALUE;
System.out.println("intNum2 = " + intNum2);
int intNum3 = Integer.MAX_VALUE + 1;
System.out.println("intNum3 = " + intNum3);
long longNum = (long)Integer.MAX_VALUE + 1;
System.out.println("longNum = " + longNum);
}
}
ex) 실수
1
2
3
4
5
6
7
8
9
10
11
// Java 프로그래밍 - 변수와 자료형
public class Main {
public static void main(String[] args) {
//1-2. 실수
float floatNum = 1.23f; //f를 붙여야 float을 인식. 아니면 double임
double doubleNum = 1.23;
System.out.println(Float.MAX_VALUE);
System.out.println(Double.MAX_VALUE);
}
}
- 위 : 3.4028235 * 10^38
- 아래 : 1.7976931348623157 * 10^308
ex) 2진수 / 8진수 / 16진수
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Java 프로그래밍 - 변수와 자료형
public class Main {
public static void main(String[] args) {
// 1-3 2진수 / 8진수 / 16진수
int numBase2 = 0b1100;
System.out.println("numBase2 = " + numBase2);
int numBase8 = 014;
System.out.println("numBase8 = " + numBase8);
int numBase16 = 0xC;
System.out.println("numBase16 = " + numBase16);
System.out.println("0b" + Integer.toBinaryString(numBase2)) ;
System.out.println("0" + Integer.toOctalString(numBase8)) ;
System.out.println("0x" + Integer.toHexString(numBase16)) ;
}
5. 자료형 - 부울 (boolean)
- 참과 거짓을 나타내는 자료형
1
2
boolean isPass = true;
boolean isOk = false;
6. 자료형 - 문자 (Character)
- 한 개의 문자 표현에 사용하는 자료형
1
2
char keyFirst = 'a';
char keyLast = 'z';
ex) 문자형
1
2
3
4
5
6
7
8
9
10
11
12
13
// Java 프로그래밍 - 변수와 자료형
public class Main {
public static void main(String[] args) {
// 3. 자료형 - 문자
System.out.println("== 문자 ==");
char keyFirst = 'a';
System.out.println("keyFirst = " + keyFirst);
char keyLast = 'z';
System.out.println("keyLast = " + keyLast);
System.out.println((int)keyFirst);
System.out.println((int)keyLast);
}
This post is licensed under CC BY 4.0 by the author.