window + shift + s : 캡쳐
환경변수 설정
path
는 운영체제와 연결되기 때문에 새로만들기 XXX
%JAVA_HOME%\bin
첫글자는 대문자 원칙
class -> package -> project
ctrl +
+
/-
: 폰트 조절 가능
줄 순서 변경 :
alt
+ 방향키
- 윈도우 시스템 키 :
ctrl
+alt
+ 방향키 때문에 작동 X
syso ->
ctrl
+space
System.out.println();
public class Hello {
public static void main(String[] args) {
System.out.println(100); // 정수
System.out.println('한'); // 문자
System.out.println("호랑이"); // 문자열
System.out.println();
System.out.println(100 + 200);// : 숫자 + 숫자 - 산술연산
System.out.println(100 + "호랑이");// : 숫자 + 문자열 - 문자열 (호랑이 + 100도 도일)
System.out.println('한' + "호랑이");// : 문자 + 문자열 - 문자열
System.out.println("호랑이" + "사자"); // : 문자열 + 문자열 - 문자열
System.out.println("100" + "문자열"); // : 문자열 + 문자열 - 문자열
System.out.println(10 + 20 + "호랑이"); // : 순서대로 실행 - 숫자 / 문자열
System.out.println("호랑이" + "사자" + 100); // : 문자열로 전부 붙어버림
System.out.println(10 + " " + 20); // : 숫자 + 공백 - 문자열
System.out.println(10 + 'A'); // : 숫자 + 문자 - 아스키 코드 연산
System.out.println(1 + 'A'); // : 숫자 + 문자 - 아스키 코드 연산
System.out.println((char)(1 + 'A')); // : char로 명시적 변환 - type casting
}
}
//ex3))
public class Hello {
public static void main(String[] args) {
System.out.println(17 + 3);
System.out.println(17 - 3);
System.out.println(17 * 3);
System.out.println(17 / 3);
System.out.println(17 % 3); // ? % n => 0 ~ n-1
// 1234 % 10 -> 자릿수 뽑기
System.out.println(3 + 4 * 5); // *, / (1순위) +, - (2순위)
System.out.println((3 + 4) * 5); // () (0순위)
System.out.println(2 + 3 * 4 + 5);
System.out.println((2 + 3) * (4 + 5));
}
}
//ex4))
public class Hello {
public static void main(String[] args) {
System.out.println(10 > 5);
System.out.println(10 >= 5);
System.out.println(10 < 5);
System.out.println(10 <= 5);
System.out.println(10 == 5);
System.out.println(10 != 5);
}
}
A B C 투표 - C 반장
조건 : A and B
조건 : A or B
//ex5))
public class Hello {
public static void main(String[] args) {
//or
System.out.println(false || false);
System.out.println(false || true);
System.out.println(true || false);
System.out.println(true || true);
System.out.println("---------------");
//and
System.out.println(false && false);
System.out.println(false && true);
System.out.println(true && false);
System.out.println(true && true);
}
}
산술연산 > 관계연산 > 논리연산
( 3 + 2 > 7 && 2 + 6 < 8 )
1.3 + 2
,2 + 6
2.> 7
,< 8
3.&&
! (x)
- 괄호 안에 x(false)가 있습니까?
GameOver
=>T/F
- GameOver의 반대의 의미가 X, 따라서
!GameOver
사용
경우의 수 : 2ⁿ
1 = 2
2 = 4
3 = 8..... 8 = 256
전구 다 꺼진게 시작 : 0 0 0
상태 값: 8개
표현 가능 조합: 256개 (0 ~ 256)
음수 표현하고 싶어짐
- 0 1 2 3 | 4 5 6 7
-4 -3 -2 -1 | 1 2 3 4
- 양수에서 0을 포기
- -2ⁿ⁻¹~ 2ⁿ⁻¹ -1
- -2³¹ ~ -2³¹ -1
//ex7))
public class Hello {
public static void main(String[] args) {
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
}
}
줄맞춤 :
ctrl
+i
정수 관련: byte 1byte, short 2byte, int 4byte, long 8byte
메모리 1, 2, 3, 4
20억 안넘으면 기본적으로 int
char와 short의 차이
char는 0부터 6만까지
short는 -3만부터 3만까지
boolean 1byte, char 2byte, float 4byte, double 8byte
논리값: boolean
문자: char
기본 소수점: float
메모리 두배 소수점: double
float와 double의 오차
ctrl
+shift
+/
: 전체 주석
ctrl
+shift
+\
: 주석 해제
[표기법]
- 카멜 표기법
int bananaNumber = 10;
- 스네이크 표기법
int banana_number = 10;
- 파스칼 표기법
int BananaNumber = 10;
- 헝가리언 표기법
str strName = "홍길동";
- 케밥 표기법
함수 인수 -> 파라미터와 대입연산
func(a){}
func(b)
a = b
a = b++
a = ++b
//이것도 같은 개념 String s = new String(); f(String s){} f(new String())
//ex11))
public class Hello {
public static void main(String[] args) {
int n = 10;
n = 20;
System.out.println(n);
// ①
n = n + 10;
System.out.println(n);
// ②복합 대입 연산자 - 얘가 컴파일 속도가 더 빠름
//실행 속도는 동일
n += 10;
System.out.println(n);
//assembly - add, move
n = n + 10;
n += 10;
//assembly - increase (훨씬 빠르다)
n++; //단항 연산자 - 실행 속도가 제일 빠름
++n;
n = 10;
int a, b;
a = n++; // 대입 후 증가 -> a = 10, n = 11
System.out.println(a + " " + n);
n = 10;
b = ++n; // 증가 후 대입 -> b = 11, n = 11
System.out.println(b + " " + n);
}
}
//ex12))
public class Hello {
public static void main(String[] args) {
int a = 10, b = 20;
int t;
System.out.println(a + " " + b);
//swap
t = a; // t = 10
a = b; // a = 20
b = t; // b = 10
System.out.println(a + " " + b);
}
}
if
for
while
switch
do while: 사용 X
c -> 알고리즘을 수반
java -> X : 알고리즘의 라이브러리화
(){}
1. if() {}
public class Hello {
public static void main(String[] args) {
if(true) {
System.out.println(1);
System.out.println(2);
}
if(false) { //unreached 코드
System.out.println(3);
System.out.println(4);
}
System.out.println(5);
if(true) {
System.out.println(6);
if(true) {
System.out.println(7);
}
System.out.println(8);
}
System.out.println(9);
}
}
2. if() {} else {}
public class Hello {
public static void main(String[] args) {
//1. if() {}
//2. if() {}else {}
//3. if() {} else if() {} else {}
int num = 2;
if(num %2 == 0) {
System.out.println("Even");
}else {
System.out.println("Odd");
}
int password = 1234;
if(password != 1234) {
System.out.println("틀림");
//break;
//return;
//exit();
//continue;
}else {
System.out.println("맞음");
System.out.println("유저가 로그인 하였습니다.");
System.err.println("다음 정보 확인");
}
int nn = 10;
if(nn < 0) {
nn = -nn; //양수화
}else { //if 문에 안걸렸을 때 속도가 증가 (안정화)
nn = +nn;
}
}
}
3. if() {} else if() {} else {}
수학 라이브러리가 가장 무게가 크다.
따라서 else문으로 잡아서 if문에 걸리지 않은 것을 잡아주는 것이 좋다.
public class Hello {
public static void main(String[] args) {
//1. if() {}
//2. if() {}else {}
//3. if() {} else if() {} else {}
int score = 87;
if(score >= 90) {
System.out.println('A');
}else if(score >= 80){
System.out.println('B');
}else if(score >= 70){
System.out.println('C');
}else {
System.out.println('F');
}
System.out.println(5);
}
}
for ->
ctrl
+space
: 자동 완성
for문 작동 순서
//ex14))
public class Hello {
public static void main(String[] args) {
//유형①
for (int i = 0; i < 4; i++) {
System.out.println(i);
}
//유형②
for (int i = 0; i <= 4; i++) {
System.out.println(i);
}
//유형③
//원점 기준으로 좌표축 위함
//최적화를 위해서 사용
//조건 * 2 + 1번 작동
for (int i = -3; i <= 3; i++) {
System.out.println(i);
}
}
}
int num = 5;
for (int i = 0; i < 10; i++) {
System.out.println(num + " * " + i + " = " + num * i);
}
int sum = 0;
int want = 1000;
for (int i = 1; i <= want; i++) {
sum += i;
System.out.println(sum);
}
public class Hello {
public static void main(String[] args) {
int sum = 1;
int a = 3, b = 4;
//a의 b승
for (int i = 0; i < b; i++) {
sum *= a;
}
System.out.println(sum);
}
}
public class Hello {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println("-------------");
for (int j = 0; j < 4; j++) {
System.out.println(1);
}
System.out.println("*************");
}
}
}
public class Hello {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) { //세로
for (int j = 0; j < 4; j++) { //가로
System.out.print("["+i+" "+j+"]");
}
System.out.println();
}
System.out.println("------------");
int width = 8, height = 7;
for (int i = 0; i < height; i++) { //세로
for (int j = 0; j < width; j++) { //가로
if((i+j) % 2==0) {
System.out.print("O ");
}else {
System.out.print("X ");
}
}
System.out.println();
}
}
}
//ex16)
public class Hello {
public static void main(String[] args) {
int result;
if(3 > 2) {
result = 10;
}else {
result = 20;
}
//무조건 삼항연산자
result = (3 > 2) ? 10 : 20;
//System.out.println((i + j) % 2 == 0 ? "O" : "X");
}
}
int func() {
if(3 > 2) {
return 100;
}else {
return 200;
}
return (3 > 2) ? 100 : 200;
}
public class Hello {
public static void main(String[] args) {
int num = 0;
while(true) {
num++;
System.out.println(num);
}
}
}
[강제 종료외에 상태 확인 - Debug]
- 무한루프 상에서 계속 프로그램을 실행하면 debug 창이 계속 생성됨
- 프로세스가 살아있는 상태로 남아있을 수 있기에 debug에서 확인필요
//ex18))
public class Hello {
public static void main(String[] args) {
int num = 0;
while(true) {
if(num == 4) {
System.out.println("탈출전");
break;
//System.out.println("unreach code");
}
System.out.println(num);
num++;
}
}
}
반복 횟수를 모르는 경우에만 while을 사용하도록
public class Hello {
public static void main(String[] args) {
int num = 17;
while(true) {
System.out.println(num);
/*if(num % 2 == 0)
num = num / 2;
else
num = num * 3 + 1;*/
//삼항 연산자로 한다면
num = (num % 2 == 0 ) ? num / 2 : num * 3 + 1;
if(num == 1) {
System.out.println(num);
break;
}
}
}
}
//ex20))
public class Hello {
public static void main(String[] args) {
Random rnd = new Random();
int num = rnd.nextInt();
if(num < 0) {
num = -num;
}
System.out.println(num);
// random 숫자 각 자릿수 더하기
int sum = 0;
while(true) {
sum += num % 10;
num = num / 10;
System.out.println(sum);
if (num <= 0) break;
}
}
}
Random rnd = new Random();
rnd.nextInt(10) //0~9 사이의 숫자
rnd.nextInt(2) //0~1 사이의 숫자
1. switch 안에 변수를 사용할 수 있다.
2. case 뒤에 변수를 사용할 수 없다.
int num = 20;
int nn = 10;
switch (num) {
//case nn: (x)
case 10:
System.out.println(1);
break;
case 20:
System.out.println(2);
break;
case 30:
System.out.println(3);
break;
default:
System.out.println("호랑이");
break;
}
3. break를 생락하면 switch를 빠져나오지 못한다.
4. 만족하는 분기문이 없을 때는 default로 이동한다.
5. 정수, 문자, 문자열 모두 분기가 가능하다.
int num = 20;
int nn = 10;
char ch = '한';
switch ("호랑이") {
//case nn: (x)
case "호랑이":
System.out.println(1);
break;
case "코끼리":
System.out.println(2);
break;
case "독수리":
System.out.println(3);
break;
default:
System.out.println("호랑이");
break;
}
6. 의도적으로 break를 생략하기도 한다.
public class Hello {
public static void main(String[] args) {
int score = 87;
switch (score / 10) {
case 10: //break; -> 암묵적으로 의도적이라는 것을 알리는 문구
//break가 없어 10도 9와 동일한 결과를 반환
case 9:
System.out.println("A");
break;
case 8:
System.out.println("B");
break;
case 7:
System.out.println("C");
break;
case 6:
System.out.println("D");
break;
default:
System.out.println("F");
break;
}
}
}
//ex23))
public class Hello {
public static void main(String[] args) {
Random rnd = new Random();
int num = rnd.nextInt(30); //0~29사이의 숫자
switch (num % 2) {
case 0:{ //여러줄이면 블록처리
for (int i = 0; i < 10; i++) {
System.out.println(num + " * " + i + " = " + num * i);
}
}break;
case 1:{
int sum = 0;
for (int i = 1; i <= num; i++) {
sum += i;
}
System.out.println(num + "까지의 합은: "+ sum);
}break;
default: break; //조건 없으면 단문
}
}
}