意外に忘れる!? Javaの数値型と文字列の型変換まとめ

こんにちは。横尾です。
 
Javaで開発を行っていると、慣れてきても何度か調べてしまうのが、型変換についてですよね。ということで、今回は数値型の型変換をまとめてみようと思います。

int型とString型の変換

int型からString型

// valueOfを使って変換
String str = String.valueOf(i);

// 空文字を足し算
String str = "" + i;

// toStringを使って変換(1)
String str = new Integer(i).toString();

// toStringを使って変換(2)
String str = Integer.toString(i);

 
String型からint型
// parseIntを使って変換
int i = Integer.parseInt(str);

// intValueを使って変換(1)
int i = new Integer(str).intValue();

// intValueを使って変換(2)
int i = Integer.valueOf(str).intValue();

short型とString型の変換

short型からString型

// valueOfを使って変換
String str = String.valueOf(s);

// 空文字を足し算
String str = "" + s;

// toStringを使って変換(1)
String str = new Short(s).toString();

// toStringを使って変換(2)
String str = Short.toString(s);

 
String型からshort型
// parseShortを使って変換
short s = Short.parseShort(str);

// shortValueを使って変換(1)
short s = new Short(str).shortValue();

// shortValueを使って変換(2)
short s = Short.valueOf(str).shortValue();

byte型とString型の変換

byte型からString型

// valueOfを使って変換
String str = String.valueOf(b);

// 空文字を足し算
String str = "" + b;

// toStringを使って変換(1)
String str = new Byte(b).toString();

// toStringを使って変換(2)
String str = Byte.toString(b);

 
String型からbyte型
// parseByteを使って変換
byte b = Byte.parseByte(str);

// byteValueを使って変換(1)
byte b = new Byte(str).byteValue();

// byteValueを使って変換(2)
byte b = Byte.valueOf(str).byteValue();

long型とString型の変換

long型からString型

// valueOfを使って変換
String str = String.valueOf(l);

// 空文字を足し算
String str = "" + l;

// toStringを使って変換(1)
String str = new Long(l).toString();

// toStringを使って変換(2)
String str = Long.toString(l);

 
String型からlong型
// parseLongを使って変換
long l = Long.parseLong(str);

// longValueを使って変換(1)
long l = new Long(str).longValue();

// longValueを使って変換(2)
long l = Long.valueOf(str).longValue();

float型とString型の変換

float型からString型

// valueOfを使って変換
String str = String.valueOf(f);

// 空文字を足し算
String str = "" + f;

// toStringを使って変換(1)
String str = new Float(f).toString();

// toStringを使って変換(2)
String str = Float.toString(f);

 
String型からfloat型
// parseLongを使って変換
float f = Float.parseFloat(str);

// floatValueを使って変換(1)
float f = new Float(str).floatValue();

// floatValueを使って変換(2)
float f = Float.valueOf(str).floatValue();

double型とString型の変換

double型からString型

// valueOfを使って変換
String str = String.valueOf(d);

// 空文字を足し算
String str = "" + d;

// toStringを使って変換(1)
String str = new Double(d).toString();

// toStringを使って変換(2)
String str = Double.toString(d);

 
String型からdouble型
// parseLongを使って変換
double d = Double.parseDouble(str);

// doubleValueを使って変換(1)
double d = new Double(str).doubleValue();

// doubleValueを使って変換(2)
double d = Double.valueOf(str).doubleValue();

数値型のキャスト

次に数値型のキャストです。
 
int型から他数値型

// int型からshort型
short S = (short)i;

// int型からbyte型
byte b = (byte)i;

// int型からlong型
long l = i;

// int型からfloat型
float f = i;

// int型からdouble型
double d = i;

short型とbyte型は、int型よりサイズが小さいため、明示的なキャストが必要になります。浮動小数点数型から整数型に代入するときも、小数の部分が切り捨てられるため、明示的なキャストが必要です。
 
小さいサイズから大きいサイズに代入するときは、特にキャストは必要ありません。

long型、double型、float型の宣言

int型を越えるlong型、double型の範囲の数値をプログラムの中で記述する場合は、数値の最後に「L」または「l」、「D」または「d」を付けます。
 
浮動小数点数の場合、数値の最後に「F」または「f」を付けると、float型として扱われます。何も付けない場合は、暗黙的にdouble型として処理されます。ただし、「D」または「d」を付けて、明示的にdouble型として宣言することもできます。
 

// long型の宣言
long l = 3000000000L;

// double型の宣言
double d = 3000000000D;

// float型の宣言
float f = 1.23F;

まとめ

今回は数値と文字列の型変換をまとめました。
 
Javaには型変換がたくさん存在するので、他の型変換もまとめてみたいと思います。

記事をシェア
MOST VIEWED ARTICLES