アットウィキロゴ

ラッパークラス

★ラッパークラス
■基本データ型のラッパークラス
ラッパークラスは基本データ型(整数型、文字型、論理型)に対応している。
基本データ型
ラッパークラス
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
 
■数値型のラッパークラス
数値型(Byte,Short,Integer,Long,Float,Double,)は次のメソッドを使用して、他の型に変換した値が得られる
byteValue()    shortValue()    intValue()
longValue()    floatValue()     doubleValue()
 
Integer a = new Integer(30); コンストラクタは対応する基本データ型の値や変数を受け取る
Double b = a.doubleValue();
※オブジェクトの比較
equals()メソッド・・・ラッパークラス同士を比較する
Integer a = new Integer(10);
Integer b = new Integer(5);
Boolean c = a.equals(b); ←等しければtrue
■文字列の変換
変換した値が得られる
クラス
メソッド
働き
Byte
parseByte()
文字列をbyte型に変換
Short
parseShort()
文字列をshort型に変換
Long
parseLong()
文字列をlong型に変換
Integer
parseInteger()
文字列をint型に変換
 
Int a = Integer.parseInt(“”); aの値は5になります。
 
     サンプルプログラム
class Change {
   public static void main(String[] args) {
      String a = "34";
      int b = 50;
      int c = Integer.parseInt(a);
      int d = b + c;
      System.out.println(a + "+" + b + "=" + d);
   }
}
 
最終更新:2008年03月18日 20:55