Javaちょこっとリファレンス
HOME > Byteクラス

Byteクラス

Byteクラスはbyte型のラッパークラスで、byte型をオブジェクトとして扱いたい時に使用します。

主要コンストラクタ

コンストラクタ説明
Byte(byte b)byte型からByte型を生成する
Byte(String s)String型からByte型を生成する

主要メソッド

【java.lang.Byteクラスの主要メソッド】
戻値型メソッド静的説明
byteparseByte(String a)引数の値をbyte型で返す
 (例)文字列"123"をbyte型に変換する
 byte b = Byte.parseByte("123");
intsignum(byte b)引数の値を判断し、1,0,-1のいずれかを返す
 正の場合:1、ゼロの場合:0、負の場合:-1
(例)int i = Byte.signum(-123);
 → -1
BytevalueOf(byte b)引数の値をByte型で返す
(例)Byte b = Byte.valueOf(123);
BytevalueOf(String s)引数の値をByte型で返す
(例)Byte b = Byte.valueOf("123");
bytebyteValue()Byte型をbyte型で返す
shortshortValue()Byte型をshort型で返す
intintValue()Byte型をint型で返す
longlongValue()Byte型をlong型で返す
floatfloatValue()Byte型をfloat型で返す
doubledoubleValue()Byte型をdouble型で返す
StringtoString()Byte型をString型で返す
intcompareTo(Byte b)引数の値よりも小さい場合は-1、大きい場合は1、同じ場合は0を返す
booleanequals(Byte b)引数の値と同じかどうかを返す

Byteサンプル

Byteを生成する
Byte by = new Byte(123);         //byte型から生成
Byte by = new Byte("123");       //String型から生成
Byte by = Byte.valueOf(123);     //byte型からvalueOfメソッドにて生成
Byte by = Byte.valueOf("123");   //String型からvalueOfメソッドにて生成

Byteから値を取り出す
byte b = by.byteValue();         //byte型として取り出す
short s = by.shortValue();       //short型として取り出す
int i = by.intValue();           //int型として取り出す
long l = by.longValue();         //long型として取り出す
float f = by.floatValue();       //float型として取り出す
double d = by.doubleValue();     //double型として取り出す
String str = by.toString();      //String型として取り出す

数値文字列をbyte型に変換する
byte b = Byte.parseByte("123");
2つのByteの値が同じかどうか比較する
Byte a = new Byte(123);
Byte b = new Byte(123);

boolean result = a.equals(b);
  result → true

2つのByte値の大小を比較する
Byte a = new Byte(100);
Byte b = new Byte(200);
Byte c = new Byte(200);

int result = a.compareTo(b);    //引数の方が大きい場合
  result → -1

int result = b.compareTo(a);    //引数の方が小さい場合
  result → 1

int result = b.compareTo(c);    //引数と同じ場合
  result → 0

このエントリーをはてなブックマークに追加

ラッパークラスカテゴリ

HOMEへ戻る TOPへ戻る