-
[JAVA 10] 자바 변수(Variable) ② 메모리 참조 방식 <기본형 / 참조형>Language/Java 2018. 2. 4. 20:04반응형2. 메모리 참조 방식<1> 기본형 변수 ( Primitive Type )1) Call by value2) 첫글자가 소문자3) 종류byte(1) → short(2) → int(4) → long(8) → float(4) → double(8)→ char(2) →boolean(1)cf) 자동 형변환: (boolean을 제외한) 범위가 작은 타입 → 큰타입 (왼쪽 → 오른쪽)< 잘못된 예1 >┐
[ error : incompatible types: possible lossy conversion from int to short ]
에러 : 공존할 수 없는 타입 : int 에서 byte 로 전환시에 파일 정보가 손실될 수 있다.
(함께 쓸 수 없는 타입) int 에서 short 로
(호환성이 없는 타입)
< 잘된 예1 >┐
12345678910111213class VS2{byte b = 1;short s = 2;int i = 3;VS2(){int r = b + s + i;System.out.println("r : " + r);}public static void main(String[] args){VS2 vs2 = new VS2();}}cs 4) 값의 범위- byte : (-2^7) ~ (2^7-1) 경우의 수 : 2^8- short : (-2^15) ~ (2^15-1) 경우의 수 : 2^16- char : 0 ~ (2^16-1) 경우의 수 : 2^16- int : (-2^31) ~ (2^31-1) 경우의 수 : 2^32- long : (-2^63) ~ (2^63-1) 경우의 수 : 2^64- float : ∞ 무한- double : ∞ 무한- boolean : false 또는 true< 주의 >컴파일은 되지만, byte는 -128에서 127 까지의 범위만 허용한다.
컴파일에서는 에러가 나오지 않지만, 허용 범위가 넘어가면 깨지는 현상이 발생하게 된다.
byte b = (byte)128; 를 하지 않으면 위에서 보았던 에러가 난다.
[ error : incompatible types: possible lossy conversion from int to short ]
에러 : 공존할 수 없는 타입 : int 에서 byte 로 전환시에 파일 정보가 손실될 수 있다.
하지만 범위가 -128~127까지 허용되기 때문에 byte b = 127; 은 가능하다.
int 로 자동 변환 되어 보여주기 때문이다.
<잘된 예2>┐
123456789101112131415161718192021222324252627class VS3{byte b = 127; // -128 ~ 127까지 허용// 127이상일때에는 깨짐 현상이 생긴다.void m1(){ // 기본형System.out.println("b : " + b);}void m2(){ // 실수형long lo = 123456789123456789L;System.out.println("lo : " + lo);float f = lo;System.out.println("f : " + f);double d = lo;System.out.println("d : " + d);double dd = 123456789123456789123456789123456789.123456789;System.out.println("dd : " + dd);}public static void main(String[] args){VS3 vs3 = new VS3();vs3.m1();vs3.m2();}}cs float 과 double 은 소수자리를 표현하지만
오차가 발생한다. float 보다는 double이 더 오차가 적다.
하지만 내가 알기로는 오차가 안나오지는 않는다.
5) 형변환 필요성→ 연산시 같은 type끼리만 가능6) 초기값- 정수형 (byte , short , char , int): 0 , 0L- 실수형 (float , double): 0.0f , 0.0- 불린형 (boolean): false<잘된 예3>┐12345678910111213141516171819202122232425class VS4{byte b;short s;char c;int i;long lo;float f;double d;boolean flag;void m(){System.out.println(" byte : " + b);System.out.println(" short : " + s);System.out.println(" char : " + (int)c);System.out.println(" int : " + i);System.out.println(" long : " + lo);System.out.println(" float : " + f);System.out.println(" double : " + d);System.out.println("boolean : " + flag);}public static void main(String[] args){new VS4().m();}}cs long 과 float 은 값을 직접 입력 할때에는 L 과 f 를 꼭 붙여야 한다
ex) long lo = 10L;
float f = 1.1f;
7) 존재이유- 실행의 효율성<잘된 예4>┐12345678910111213141516171819202122class VS5{String number = "100";void m1(){ // 범위 최소/최대값 확인int max = Integer.MAX_VALUE; // (-2^31)int min = Integer.MIN_VALUE; // (2^31-1)System.out.println(" max : " + max);System.out.println(" min : " + min);System.out.println("");}void m2(){int i = Integer.parseInt(number);System.out.println(" number r : " + (i + 11));}public static void main(String[] args){VS5 vs = new VS5();vs.m1();vs.m2();}}//cs 자바는 객체 지향 언어이기 때문에 모든 것을 객체로 만들어야 하는데
1 + 2 = 3 로 예를 들자면
1객체를 만든다. → 2객체를 만든다. → 객체를 더해서 3이 나온다.
라는 흐름은 굉장히 비효율적이기 때문에 기본형 타입은 실행의 효율성을 위해서 예외가 된다.
각각의 MAX_VALUE; MIN_VALUE; 를 사용하면 허용 범위를 직접 확인할 수 있다.
8) 예외 형변환→ 정수형(byte , short , char , int)끼리의 연산의 결과는 int위의 <잘된 예1> 에서 설명하였다.9) 컴퓨터의 소수 오차float 와 double 은 소수형으로 무한대까지 보여줄수는 있다.double 이 float 보다 오차가 적다.하지만 오차가 없지는 않다.<2> 참조형 변수( Reference Type )
1) Call by reference
2) (일반적)첫글자가 대문자
3) 기본형8가지를 제외한 모든 Type
4) 초기값 : null
5) 형변환
ⓐ 자동 형변환(up casting)
: Human hm = new Superman();
Object ← Human ← Superman (상속성에 대해 설명글 참조)1
ⓑ 강제 형변환(down casting)1
: Superman sm = (Superman)hm;
Object → Human → Supermam
반응형'Language > Java' 카테고리의 다른 글
[JAVA 12] 자바 변수(Variable) ④ final (초기값 변경) 유무 (2) 2018.02.04 [JAVA 11] 자바 변수(Variable) ③ static (소속/소유) 유무 (0) 2018.02.04 [JAVA 09] 자바 변수(Variable) ① 유효 범위 (2) 2018.02.04 [JAVA 08] 자바 OOP의 특성 ④ 캡슐화 ( Encapsulation ) (0) 2018.01.28 [JAVA 07] 자바 OOP의 특성 ③ 은닉성 ( Information Hiding ) (2) 2018.01.28