자바의 정석 - 래퍼클래스, Number클래스

Yohan·2024년 2월 6일
0

래퍼(wrapper) 클래스

  • 기본형 값을 감싸는 클래스
  • 8개의 기본형을 객체로 다뤄야할 때 사용하는 클래스
    • 기본형 : boolean, char, byte, short, int, long, float, double
    • 래퍼클래스 : Boolean, Character, Byte, Short, Integer, Long, Float, Double
  • 예시
Boolean b = new Boolean(true);
Character c = new Character('c'); // 다 이런식
  • 예제
public class Ex1 {
	public static void main(String[] args) {
		Integer i = new Integer(100);
		Integer i2 = new Integer(100);
		
		System.out.println("i==i2 ? "+(i==i2)); // 주소비교
		System.out.println("i.equals(i2) ? "+i.equals(i2)); // 내용비교
		System.out.println("i.compareTo(i2) ? "+i.compareTo(i2)); 
		System.out.println("i.toString() ? "+i.toString()); 
	}
}

i==i2 ? false
i.equals(i2) ? true
i.compareTo(i2) ? 0
i.toString() ? 100

Number 클래스

  • 모든 숫자 래퍼클래스의 조상
profile
백엔드 개발자

0개의 댓글