wrapper를 쓰면 null을 저장할 수 있다.
wrapper를 쓰면 함수를 사용할 수 있다.
Long d = 10L; (O) (Long d = 10; (X))
1. null 저장 가능
package ex17;
class User {
// 연산이 아니라 데이터를 저장하려는 용도 -> int에 null 어떻게?
private Long no;
private String name;
private Integer money;
public User(Long no, String name, Integer money) {
this.no = no;
this.name = name;
this.money = money;
}
public Long getNo() {
return no;
}
public String getName() {
return name;
}
public Integer getMoney() {
return money;
}
}
// 왜 써야하는지 알면 됨
public class Wrap01 {
public static void main(String[] args) {
// 1. 회사 운영
// null을 넣으려면 Wrapper 클래스에 넣어야 함
// null을 넣을 수 있으려면 참조 변수 즉 클래스 타입이어야 한다.
User u = new User(1L, "홍길동", null);
}
}
2. 함수 사용 가능
package ex17;
public class Wrap02 {
public static void main(String[] args) {
String s1 = "10";
Integer i1 = Integer.parseInt(s1); // parseInt(): 문자열 -> 숫자
Integer i2 = 20;
String s2 = i2 + "";
String s3 = i2.toString(); //i2는 Integer, Object가 toString을 가지고 있지만 Integer가 재정의함 -> 즉, 동적 바인딩에 의해 Object가 가진 toString이 아니라 Integer가 재정의한 toString임.
}
}
Share article