Wrapper : 기초 자료형을 객체로 사용하기 위해 사용하는 클래스
Wrapper
자료형 | Wrapper |
int | Integer |
short | Short |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
byte | Byte |
package ex17;
class User {
// Long 클래스 : Long l1 = 10L;
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) {
// Wrapper : 기초 자료형이 가진 함수 사용 가능
// Wrapper의 장점 1. 빈 값(null)을 직접 넣을 수 있음 (다른 값으로 대체되지 않음)
User user = new User(1L, "Jack", null);
}
}
Wrapper 함수
Integer.parseInt() : 문자를 숫자로 변환
Integer.toString : 숫자를 문자로 변환
- int + “” 로 가능
package ex17;
public class Wrap02 {
public static void main(String[] args) {
String s1 = "10";
// 문자를 숫자로 변환 : Integer.parseInt()
Integer i1 = Integer.parseInt(s1);
System.out.println(i1);
// 숫자를 문자로 변환 1. "" 추가
Integer i2 = 20;
String s2 = i2 + "";
// 숫자를 문자로 변환 2. toString()
String s3 = i2.toString();
}
}
Share article