안녕하세요, 여행벌입니다.

자바 Wrapper 클래스에 이어서 Number 클래스에 대해서도 정리해보겠습니다.


Number 클래스

 Character, Boolean 을 제외하고 모든 Wrapper 클래스는 Number 클래스를 상속합니다. 그리고 Number 클래스는 추상 클래스로, Wrapper 클래스마다 Number 클래스의 추상 메소드들을 이미 잘 구현해놓았습니다. 즉, Number 클래스는 Wrapper 클래스에게 공통된 기능들을 제공해주기 위해 존재하는 클래스입니다.

 

Number 클래스에는 다음과 같은 추상 메소드들이 존재합니다.

public abstract int intValue()

 intValue() 메소드는 wrapper 클래스 안에 있는 기본 자료형 값을 int 형으로 변환해서 return 해주는 메소드입니다.

1
2
3
4
5
6
7
public class test{
    public static void main(String args[]) {
        System.out.println("Byte 클래스 : " + new Byte((byte1).intValue());
        System.out.println("Integer 클래스 : " + new Integer(5).intValue());
        System.out.println("Double 클래스 : " + new Double(5.5).intValue());
    }
}
cs
1
2
3
Byte 클래스 : 1
Integer 클래스 : 5
Double 클래스 : 5
cs

public abstract long longValue()

 longValue() 메소드는 wrapper 클래스 안에 있는 기본 자료형의 값을 long 형으로 변환해서 return 해주는 메소드입니다. 

1
2
3
4
5
6
7
public class test{
    public static void main(String args[]) {
        System.out.println("Byte 클래스 : " + new Byte((byte2).longValue());
        System.out.println("Integer 클래스 : " + new Integer(7).longValue());
        System.out.println("Double 클래스 : " + new Double(56.5).longValue());
    }
}
cs

public abstract float floatValue()

 floatValue() 메소드는 wrapper 클래스 안에 있는 기본 자료형의 값을 float 형으로 변환해서 return 해주는 메소드입니다.

1
2
3
4
5
6
7
public class test{
    public static void main(String args[]) {
        System.out.println("Byte 클래스 : " + new Byte((byte2).floatValue());
        System.out.println("Integer 클래스 : " + new Integer(7).floatValue());
        System.out.println("Double 클래스 : " + new Double(56.5).floatValue());
    }
}
cs

public abstract double doubleValue()

 doubleValue() 메소드는 wrapper 클래스 안에 있는 기본 자료형의 값을 double 형으로 변환해서 return 해주는 메소드입니다.

1
2
3
4
5
6
7
public class test{
    public static void main(String args[]) {
        System.out.println("Byte 클래스 : " + new Byte((byte2).doubleValue());
        System.out.println("Integer 클래스 : " + new Integer(7).doubleValue());
        System.out.println("Double 클래스 : " + new Double(56.5).doubleValue());
    }
}
cs

public byte byteValue()

 byteValue() 메소드는 Wrapper 클래스 안에 있는 기본 자료형의 값을 byte 형으로 변환해서 return 해주는 메소드입니다.

1
2
3
4
5
6
7
public class test{
    public static void main(String args[]) {
        System.out.println("Byte 클래스 : " + new Byte((byte2).byteValue());
        System.out.println("Integer 클래스 : " + new Integer(7).byteValue());
        System.out.println("Double 클래스 : " + new Double(56.5).byteValue());
    }
}
cs

public short shortValue()

 shortValue() 메소드는 Wrapper 클래스 안에 있는 기본 자료형의 값을 short 형으로 변환해서 return 해주는 메소드입니다.

1
2
3
4
5
6
7
public class test{
    public static void main(String args[]) {
        System.out.println("Byte 클래스 : " + new Byte((byte2).shortValue());
        System.out.println("Integer 클래스 : " + new Integer(2).shortValue());
        System.out.println("Double 클래스 : " + new Double(6.5).shortValue());
    }
}
cs

Wrapper 클래스 중에서 Character, Boolean 클래스를 제외하고

모두 Number 클래스를 상속하고 추상 메소드들을 구현해놓았다는 점!

을 다시 한 번 상기하며 이상으로 포스팅을 마치겠습니다.

 

 

+ Recent posts