ためになるホームページ お問い合わせ




TOP > Java > ラッパークラス
ラッパークラスの概要
基本型の値をオブジェクト内に「ラップ」する仕組みを提供する事で、基本型の値をオブジェクトと同様に操作できるようにする。
基本型ユーティリティ機能一式を提供する。ラッパーオブジェクトは不変性であり、設定した値は二度と変更できない。

ラッパークラスの種類とコンストラクタ
基本型 ラッパークラス コンストラクタ引数
int Integer int または String
short Short short または String
long Long long または String
byte Byte byte または String
float Float float , double または String
double Double double または String
boolean Boolean boolean または String
char Character char

ラッパークラスとコンストラクタの例
class Test{
    public static void main(String[] args){
        boolean a = true ;
        Boolean b = new Boolean(a) ;

        /*コンパイルエラー
        /bはブール式ではない
        /Booleanオブジェクトである
        if(b){}
        */
        Character c = new Character('c') ;

        /*このコードはコンパイル不可
        Character c = new Character(c) ;
        Character c = new Character("c") ;
        */

        byte d = 125 ;
        Byte e = new Byte(d) ;

        /*実行時、NumberFormatExceptionが投げられる。
        /
        Byte e = new Byte("128") ;
        */

        Short f = new Short((short)100) ;

        /*このコードはコンパイル不可
        /int型の値を入れている。
        Short f = new Short(100) ;
        */

        Integer g = new Integer(100) ;
        Integer h = new Integer("123") ;

        /*このコードはコンパイル不可
        Integer h = new Integer(100L) ;
        Integer h = new Integer(1.23) ;
        */

        Long i = new Long(123) ;
        Long j = new Long(123L) ;
        //これも可
        //charも所詮ビットパターンだから。
        Long k = new Long('c') ;
        System.out.println(k) ;//99

        Float l = new Float(1) ;
        Float m = new Float(1L) ;
        Float n = new Float(1.23D) ;

        Double o = new Double(1.23f) ;
        Double p = new Double('c') ;
        Double q = new Double("111.1111") ;

        /*実行時、NumberFormatExceptionが投げられる。
        /
        Double r = new Double("tanaka") ;
        */
    }
}


valueOf()メソッドでオブジェクトの作成
指定された String の値を保持するオブジェクトを返す。静的メソッドなので、「クラス名.メソッド名」で呼び出す。BooleanのvalueOf()メソッドのみ「ブール値」をとれる。2番目の引数で指定された基数(2進数、8進数、16進数)を使用した構文解析時に、指定された String から抽出された値を保持するオブジェクトを返す。(当然浮動小数点には使えない!)
  • String型だけを引数に取るクラス・・・Character以外
  • 2番目の引数をとる・・・Byte、Short、Integer、Long

  • valueOfメソッドの例
    class Test{
        public static void main(String[] args){
            Integer i = Integer.valueOf("11111") ;
            /*コンパイル不可。
            /引数はStringのみ
            Integer i = Integer.valueOf(11111) ;
            */
    
            //第二引数を使い、2進数を指定
            Long l = Long.valueOf("010101" ,2) ;
            System.out.println(l) ;
    
            Float f = new Float("1.11f") ;
            System.out.println(f) ;
    
            //これは、(true)でも可
            Boolean b = Boolean.valueOf("true") ;
            System.out.println(b) ;
        }
    }
    
    

    メソッド[xxxxValue()メソッド]
    ラップしたオブジェクトを基本型に変換する。
    xxxxは、Byte、Short、Integer、Long、Float、Double。

    xxxxValue()メソッドの例
    class Test{
        public static void main(String[] args){
            Double d = new Double("111.1") ;
            byte b1 = d.byteValue() ;
            System.out.println(b1) ;
            Integer i1 = new Integer(111) ;
            byte b2 = i1.byteValue() ;
            Sytem.out.println(b2) ;
            Integer i2 = Integer.valueOf("111") ;
    
            /*コンパイルエラー
            /int型をbyteに入れられない
            byte b3 = i2.intValue() ;
            */
        }
    }
    
    

    メソッド[parsexxxx()メソッド]
    String型の引数をとり、それを基本型にする静的メソッド。第二引数もとれるメソッドもある。第二引数は、基数。Stringのみ受け取るメソッドは、DoubleとFloat。第二引数も受け取れるメソッドは、Integer、Long、Byte、Short。

    parsexxxx()メソッドの例
    class Test{
        public static void main(String[] args){
            byte a = Byte.parseByte("123") ;
            System.out.println(a + 3) ;
    
            /*コンパイルエラー
            /parseIntの戻り値型は、int
            byte b = Integer.parseInt("23") ;
            */
    
            short c = Short.parseShort("0011111" , 2) ;
    
            double d = Float.parseFloat("1.23F") ;
            System.out.println(d) ;
        }
    }
    
    

    メソッド[toxxxString()メソッド]
    IntegerとLongには、toxxxString()静的メソッドがある。「xxx」には、「Binary」「Hex」「Octal」が入る。このメソッドは、整数の引数の文字列表現を、基数の符号なし整数として返す。戻り値の型はString型。
    負数を引数でとった場合、Integerは2の32乗が加算される。Longは2の64乗が加算される。

    toxxxString()メソッドの例
    class Test{
        public static void main(String[] args){
            System.out.println(Integer.toBinaryString(256)) ;
            System.out.println(Integer.toOctalString(256)) ;
            System.out.println(Long.toHexString(80)) ;
        }
    }
    
    

    メソッド[toString()メソッド]
    ラッパーオブジェクトをtoString()メソッドで呼び出すと、そのオブジェクトがラップしている基本型の値がString型で返される。
    どのラッパークラスにもオーバーロードされた静的toString()メソッドがある。受け取った引数基本型をString型で返す。
    IntegerクラスとLongクラスには、2つの引数をとる静的メソッドtoString()メソッドがある。このメソッドは、基本型を第一引数にとり、基数を第二引数にとる。

    toString()メソッドの例
    class Test{
        public static void main(String[] args){
            Boolean b = new Boolean(true) ;
            System.out.println(b.toString()) ;
    
            //オーバーロードされたtoString()メソッド
            //基本型をString型で返す
            System.out.println(Boolean.toString(true)) ;
    
            //引数を2つ持つtoString()メソッド
            //IntegerとLongにしかない。
            //第二引数の基数で値を返す
            System.out.println(Integer.toString(256 , 2)) ;
        }
    }
    
    






    Copyright 2007 ためになるホームページ All Rights Reserved.