Upper Camel Case
✓ 각 단어의 첫 글자는 대문자, 나머지는 소문자 처리
✓ 클래스 이름 규칙
✓ MyHome, MyProject 등
Lower Camel Case
✓ 각 단어의 첫 글자는 대문자, 나머지는 소문자 처리
✓ 가장 첫 글자는 소문자 처리
✓ 변수, 메소드 이름 규칙
✓ myHome, myProject 등
Snake Case
✓ 각 단어를 밑줄(_)로 연결
✓ 대소문자를 함께 사용할 수 없을 때 사용
✓ 상수 이름 규칙
✓ MY_HOME, MY_PROJECT 등
boolean
char
byte
, short
, int
, long
float
, double
데이터의
참조값(주소)을 저장하는 타입
배열(Array)
클래스(Class)
인터페이스(Interface)
- 배열(Array)
배열을 이용해서 여러 개의 데이터를 한 번에 나타냄
예시 :int[] points = new int[10];
- 클래스(Class)
각종 클래스를 이용해서 객체를 나타냄
예시 : String 클래스를 이용해서 문자열을 표현
String message = "안녕하세요";
- 인터페이스(Interface)
각종 인터페이스를 이용해서 구현 클래스를 표현
예시 : List 인터페이스를 이용해서 ArrayList 클래스를 표현
List<String> hobbies = new ArrayList<>();
// reference type : 참조타입
//
// ┌--------------┐
// name│ 0x12345678 │
// │--------------│
// │ ... │
// │--------------│
// │ tom │0x12345678 (= tom의 메모리 주소)
// │--------------│
// │ ... │
// └--------------┘
String name = "tom";
System.out.println(name);
• 프로그램에서 사용하는 값을 저장하기 위한 공간
• 데이터 타입에서 정한 크기의 메모리 공간을 사용해서 값을 저장
• 데이터 타입 다음에 변수 이름을 작성해서 변수를 선언(declare)한 뒤 사용
• 변수에 저장된 값은 언제든 다른 값으로 바꿀 수 있음
int 변수명 = 값; //int = 데이터 타입이 들어가는 자리
• final 키워드를 추가한 변수
• 변수를 선언할 때 반드시 초기값을 지정해야 함
• 실행 중 저장된 값을 변경하는 것이 불가능
• 변수와 구분하기 위해 대문자를 이용한 Snake Case 방식의 Naming 사용
final 데이터타입 상수명(대문자) = 초기값;
'\b'
,"\b"
: 백스페이스(backspace)'\f'
,"\f"
: 폼 피드(form feed)'\n'
,"\n"
: 라인 피드(line feed)'\r'
,"\r"
: 캐리지 리턴(carriage return)'\t'
,"\t"
: 탭(tab)'\''
,"\'"
: 작은 따옴표(single quote)'\"'
,"\""
: 큰 따옴표(double quote)'\\'
,"\\"
: 백슬래시(backslash)
프로젝트 : 맘대로
패키지 : 모두 소문자로 작성
(실무는 회사 도메인을 거꾸로 작성 : com.samsung.xxx)
클래스 : Upper Camel Case
(첫 글자가 대문자인 Camel Case)
메소드 : Lower Camel Case
(첫 글자가 소문자인 Camel Case)
변수 : Lower Camel Case
(첫 글자가 소문자인 Camel Case)
상수 : Snake Case
(대문자를 밑줄로 연결)
boolean
// primitive type - 1 : 논리타입 boolean isGood = true; boolean isAlive = false; System.out.println(isGood); System.out.println(isAlive);
char
// primitive type - 2 : 문자타입 char ch1 = 'A'; char ch2 = '가'; System.out.println(ch1); System.out.println(ch2);
int
, long
// primitive type - 3 : 정수타입 int score = 100; long money = 10000000000L; System.out.println(score); System.out.println(money);
double
// pimitive type - 4 : 실수타입 double commission = 0.5; System.out.println(commission);
final
상수// 상수 final double PI = 3.141592; // 상수의 변수명은 대문자로 작성 하는 규칙이 있다. System.out.println(PI);
👉 선생님 말씀.
byte num1 = 127; // byte는 -128 ~ 127 가능 int num2 = num1; // 1바이트 크기를 가진 num1을 4바이트 크기로 자동 변환
double addResult = 1.5 + 5; // 5를 5.0으로 자동 변환
int bigNum = 256; byte smallNum = (byte)bigNum; // 4바이트 bigNum을 강제로 1바이트로 변환 System.out.println(smallNum); // 캐스팅을 잘못하면 원본이 훼손될 수 있다.
double pct = 0.5; int iPct = (int)pct; // 실수를 강제로 정수로 변환(소수점 잘려나간다.)
int
, long
)double
)String strScore = "100"; int score = Integer.parseInt(strScore); String strMoney = "10000000000"; long money = Long.parseLong(strMoney); String strComm = "0.5"; double comm = Double.parseDouble(strComm);
int a = 5; int b = 2; int add = a + b; int sub = a - b; int mul = a * b; int mok = a / b; // 몫 (몫과 나머지가 필요시엔 정수 사용) int mod = a % b; // 나머지
double x = 5; double y = 2; double addResult = x + y; double subResult = x - y; double mulResult = x * y; double divResult = x / y; // 나누기 (소수점으로 나눠야 할 땐 실수 사용) double modResult = x % y;
int a = 10; System.out.println(++a); // a를 증가 시킨 뒤 출력한다. System.out.println(a);
int b = 10; System.out.println(b++); // b를 출력한 뒤 증가시킨다. System.out.println(b);
int a = 10; int b = 10;
int x = 10; int y = 1; y += x; // y = y + x; y값을 x만큼 늘리시오.
int a = 3; int b = 5; boolean result1 = a > b; // a가 b보다 크면 true, 아니면 false boolean result2 = a >= b; // a가 b보다 크거나 같으면 true, 아니면 false boolean result3 = a < b; // a가 b보다 작으면 true, 아니면 false boolean result4 = a <= b; // a가 b보다 작거나 같으면 true, 아니면 false boolean result5 = a == b; // a와 b가 같으면 true, 아니면 false boolean result6 = a != b; // a와 b가 다르면 true, 아니면 false
AND
: &&
, 모든 조건이 만족하면 true, 아니면 falseOR
: ||
, 하나의 조건이라도 만족하면 true, 아니면 falseNOT
: !
, 조건 결과가 true이면 false, 조건 결과가 false이면 trueint x = 10; int y = 20; boolean andResult = (x == 10) && (y == 10); // 모든 조건이 만족하지 않기 때문에 false boolean orResult = (x == 10) || (y == 10); // 하나의 조건이 만족하므로 true boolean notResult = !(x == 10); // x != 10와 동일한 조건
AND
: 결과가 false인 조건
이 나타나면 더 이상 조건을 체크하지 않는다. 최종 결과가 false로 정해졌기 때문이다.OR
: 결과가 true인 조건
이 나타나면 더 이상 조건을 체크하지 않는다. 최종 결과가 true로 정해졌기 때문이다.int i = 10; int j = 10; boolean andSceResult = (++i == 10) && (++j == 10); // 앞에서 이미 결과(false)가 정해졌기 때문에 ++j연산은 동작하지 않음. System.out.println(andSceResult); // false System.out.println(i); // 11 System.out.println(j); // 10 boolean orSceResult = (j++ == 10) || (i++ == 10); // 앞에서 이미 결과(true)가 정해졌기 때문에 i++연산은 동작하지 않음. System.out.println(orResult); // true System.out.println(i); // 11 System.out.println(j); // 11
삼항 연산
이라고도 한다.조건식 형식
조건식 ? true인 경우 결과 : false인 경우 결과;
조건식 예제
int score = 100; String result = (score >= 60) ? "합격" : "불합격"; System.out.println(result);
문자열 연결
String str1 = "구디" + "아카데미"; // 구디아카데미 String str2 = 4 + "달라"; // 4달라 String str3 = 1 + 2 + "번지"; // 3번지 System.out.println(str1); System.out.println(str2); System.out.println(str3);
정수 -> 문자열
실수 -> 문자열
String str4 = 100 + ""; // 빈 문자열("")을 더해주면 된다. String str5 = 1.5 + ""; // 빈 문자열("")을 더해주면 된다. System.out.println(str4); System.out.println(str5);
.valueOf()
String str6 = String.valueOf(100); // 잘 안 쓰이지만 존재한다. System.out.println(str6);
public static void 메소드명() { }
public static void main(String[] args) { 메소드명(); }
ctrl + shift + x