English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

원시 데이터 타입을 포장 객체에 포장하는 Java 프로그램

각 Java 기본 데이터 타입에는 특정한 클래스가 있습니다. 이 클래스는 원시 데이터 타입을 해당 클래스의 객체로 포장합니다. 따라서 이를 패키지 클래스라고 합니다.

다음은 패키지 객체에서 원시 데이터 타입을 표시하는 프로그램입니다.

예제

public class Demo {
   public static void main(String[] args) {
      Boolean myBoolean = new Boolean(true);
      boolean val1 = myBoolean.booleanValue();
      System.out.println(val1);
      Character myChar = new Character('a');
      char val2 = myChar.charValue();
      System.out.println(val2);
      Short myShort = new Short((short) 654);
      short val3 = myShort.shortValue();
      System.out.println(val3);
      Integer myInt = new Integer(878);
      int val4 = myInt.intValue();
      System.out.println(val4);
      Long myLong = new Long(956L);
      long val5 = myLong.longValue();
      System.out.println(val5);
      Float myFloat = new Float(10.4F);
      float val6 = myFloat.floatValue();
      System.out.println(val6);
      Double myDouble = new Double(12.3D);
      double val7 = myDouble.doubleValue();
      System.out.println(val7);
   }
}

출력 결과

True
a
654
878
956
10.4
12.3

위 프로그램에서는 각 데이터 타입을 하나씩 처리했습니다. 예를 들어布尔값을 보면, 우리의 패키지는-

Boolean myBoolean = new Boolean(true);

현재, 원시 데이터 타입은 패키지 객체에 포장되어 있습니다

boolean val1 = myBoolean.booleanValue();