기본타입(primitive Type) 데이터를 객체로 다뤄야할 때, 참조 타입으로 다루기위해 객체로 감싸 다루는 것
package jun;
public class Yongjun {
public static void main(String[] args) {
int a =25;
double b = (double)a/3; //Casting, 강제형변환 / 자동형변환
System.out.println(b); //8.333333333333334
String c = "25";
//int d =(int)c; //d:기본형,c:객체형 / (기본형)객체형 /error
int d = Integer.parseInt(c); //Integer.parseInt() : 문자열을 정수로 변환하는 메서드, unBoxing
System.out.println(d); //25
int e=5;
Integer f=e; //JDK 5.0, AutoBoxing(기본형 -> 객체형)
//Integer f = new Integer(e); //JDK 5.0이전,deprecate
System.out.println(f); //5
Integer g = 5;
int h=g; //JDK 5.0, unBoxing(객체형 -> 기본형)
//int h = g.intValue(); //JDK 5.0이전
System.out.println(h); //5
/*
객체형
기본형 -----> 객체형(클래스화, Wrapper Class (포장시킴))
int Integer
double Double
*/
}
}
기본 타입 자료형 (Primitive Type)
int, boolean, double, float 등등
참조 타입 자료형 (Refernce Type)
Integer, Boolean, String, Double, 배열 등등
값을 저장하는가(기본타입), 주소를 참조하는가(참조타입)
JVM내부에서 메모리가 어디 영역에 저장되는가 (stack - 기본형이 저장됨, heap - 객체들이 저장됨)
객체들이 생성될 때는, 그 객체가 힙영역에 생성되어, 생성된 주소를 참조
package jun;
public class Yongjun {
public static void main(String[] args) {
boolean a = 25>36;
System.out.println("a = "+a); //a = false
System.out.println("!a = "+!a); //!a = true
String b ="apple"; //literal 생성
System.out.println(b); //apple
String c =new String("apple"); //인스턴스화
System.out.println(c); //apple
String result = b==c ? "같다" : "다르다"; //메모리의 주소 비교
System.out.println("b == c : "+ result); //b == c : 다르다
System.out.println("b != c : "+ (b!=c ? "참" : "거짓")); //b != c : 참
String result2 = b.equals(c)? "같다" : "다르다"; //문자열 비교
System.out.println("b.equals(c) : "+result2); //b.equals(c) : 같다
System.out.println("!b.equals(c) : "+(!b.equals(c)? "참" : "거짓")); //!b.equals(c) : 거짓
}
}