728x90
0. 김영한님의 자바 강의 듣는 공부 일지
강의 주소 :
[지금 무료] 김영한의 자바 입문 - 코드로 시작하는 자바 첫걸음 | 김영한 - 인프런
김영한 | 프로그래밍에 처음 입문하는 분들을 위한 자바 강의입니다. 코드를 따라하면서 손쉽게 자바를 배울 수 있습니다., 국내 개발 분야 누적 수강생 1위, 제대로 만든 김영한의 자바 입문[사
www.inflearn.com
1. Scope - 지역 변수와 스코프
- 스코프 : 변수의 접근 가능한 범위
package scope; //패키지 선언
public class Scope1 { // 클래스 Scope1 시작
public static void main(String[] args) { // main 메서드 시작
int m = 10; // m 생존시작
if (true) {
int x = 20; // x 생존 시작
System.out.println("if m =" + m); // 블록 내부에서 블록 외부는 접근 가능
System.out.println("if x =" + x);
} // x 생존 종료
//System.out.println("main x = " + x); // 오류, 변수 x에 접근 불가
System.out.println("main m = " + m);
} // main 메서드 끝
} // 클래스 Scope1 끝
---
if m =10
if x =20
main m = 10
package scope; // 패키지 선언
public class Scope2 { // 클래스 Scope2 시작
public static void main(String[] args) { // main 메서드 시작
int m = 10;
for (int i = 0; i < 2; i++) { // 블록 내부, for문 내
System.out.println("for m =" + m); // 블록 내부에서 외부는 접근 가능
System.out.println("for i =" + i);
} // i 생존 종료
//System.out.println("main i = " + i); //오류 , i에 접근 불가
System.out.println("main m =" + m);
} // main 메서드 끝
} // 클래스 Scope2 끝
---
for m =10
for i =0
for m =10
for i =1
main m =10
2. Scope - 스코프 존재 이유
//비효율적인 메모리 사용
//코드 복잡성 증가
package scope; // 패키지 선언
public class Scope3_1 { // 클래스 Scope3_1 시작
public static void main(String[] args) { // main 메서드 시작
int m = 10;
int temp = 0;
if (m > 0) {
temp = m * 2;
System.out.println("temp = " + temp);
}
System.out.println("m =" + m);
} // main 메서드 끝
} // 클래스 Scope3_1 끝
---
temp = 20
m =10
// temp의 스코프를 꼭 필요한 곳으로 한정
package scope; // 패키지 선언
public class Scope3_2 { // 클래스 Scope3_2
public static void main(String[] args) { // main 메서드 시작
int m = 10;
if (m > 0) {
int temp = m * 2; // if 블록 안에서 선언, temp의 스코프가 if 블록 안으로 줄어듦
System.out.println("temp =" + temp);
}
System.out.println("m =" + m);
} // main 메서드 끝
} // 클래스 Scope3_2 끝
---
temp =20
m =10
3. 형변환
작은 범위에서 큰 범위로 대입은 허용.
(1) 자동 형변환
package casting; // 패키지 선언
public class Casting1 { // 클래스 Casting 시작
public static void main(String[] args) { // main 메서드 시작
int intValue = 10;
long longValue;
double doubleValue;
longValue = intValue; // int -> long
System.out.println("longValue = " + longValue);
doubleValue = intValue; // int -> double
System.out.println("doubleValue1 = " + doubleValue); // doubleValue1 = 10.0
doubleValue = 20L; // long -> double
System.out.println("doubleValue2 = " + doubleValue); // doubleValue2 = 20.0
} // main 메서드 끝
} // 클래스 Casting 끝
---
longValue = 10
doubleValue1 = 10.0
doubleValue2 = 20.0
큰 범위에서 작은 범위 대입은 명시적 형변환이 필요.
(2) 명시적 형변환
package casting; // 패키지 선언
public class Casting2 { // 클래스 Casting2 시작
public static void main(String[] args) { // main 메서드 시작
double doubleValue = 1.5;
int intValue = 0;
//intValue = doubleValue; // 컴파일 오류 발생
intValue = (int) doubleValue; // 형변환
System.out.println(intValue); // 출력:1
} // main 메서드 끝
} // 클래스 Casting2 끝
---
1
- 형변환과 오버플로우
package casting; // 패키지 선언
public class casting3 { // //클래스 casting3 시작
public static void main(String[] args) { // main 메서드 시작
long maxIntValue = 2147483647; //int 최고값
long maxIntOver = 2147483648L; //int 최고값 + 1 (초과)
int intValue = 0;
intValue = (int) maxIntValue; //형변환
System.out.println("maxIntValue casting = " + intValue); // 출력:2147483647
intValue = (int) maxIntOver; //형변환
System.out.println("maxIntOver casting = " + intValue); // 출력:-2147483648
} // main 메서드 끝
} // 클래스 casting3 끝
---
maxIntValue casting = 2147483647
maxIntOver casting = -2147483648 //int의 가장 작은 값부터 다시 시작, 오버플로우
4. 계산과 형변환
< 자바에서 계산은 다음 2가지를 기억할 것>
1. 같은 타입끼리의 계산은 같은 타입의 결과를 낸다.
2. 서로 다른 타입의 계산은 큰 범위로 자동 형변환이 일어난다.
ex) int + long => long + long , int + double => double + double
package casting; // 패키지 선언
public class Casting4 { // 클래스 Casting4 시작
public static void main(String[] args) { // main 메서드 시작
int div1 = 3 / 2;
System.out.println("div1 = " + div1); //1
double div2 = 3 / 2;
System.out.println("div2 = " + div2); //1.0
double div3 = 3.0 / 2;
System.out.println("div3 = " + div3); //1.5
double div4 = (double) 3 / 2;
System.out.println("div4 = " + div4); //1.5
int a = 3;
int b = 2;
double result = (double) a / b;
System.out.println("result = " + result); //1.5
} // main 메서드 끝
} // 클래스 Casting4 끝
---
div1 = 1
div2 = 1.0
div3 = 1.5
div4 = 1.5
result = 1.5
5. 정리
<오늘의 단축키>
⭐ 한 줄 복사 (ctl + D)
⭐ psvm (public static void main)
⭐ sout (systemout.println)
728x90
'Computer Language > JAVA' 카테고리의 다른 글
[240615] Array (0) | 2024.06.15 |
---|---|
[240614] Scanner (0) | 2024.06.14 |
[240614] Loop (1) | 2024.06.14 |
[240611] Conditional Statements (0) | 2024.06.14 |
[240610] Operator (1) | 2024.06.11 |