Byte类将原始类型byte的值包装在对象中。字节类型的对象包含一个类型为字节的字段。
以下是Byte类的一些方法-序号方法与说明1bytebyteValue()
此方法以字节形式返回此Byte的值。
2int compareTo(Byte anotherByte)
此方法以数字方式比较两个Byte对象。
3静态字节解码(String nm)
此方法将字符串解码为字节。
4doubledoubleValue()
此方法以double形式返回此Byte的值。
5boolean equals(Object obj)
此方法将此对象与指定对象进行比较。
6floatfloatValue()
此方法以浮点数形式返回此Byte的值。
7inthashCode()
此方法为此字节返回一个哈希码。
8intintValue()
此方法以int形式返回此Byte的值。
9longlongValue()
此方法返回此Byte的long值。
10静态字节parseByte(String s)
此方法将字符串参数解析为带符号的十进制字节。
现在让我们看一个例子-
示例import java.lang.*;
public class Demo {
public static void main(String[] args){
Byte b1, b2;
int i1, i2;
b1 = new Byte("1");
b2 = new Byte("-1");
i1 = b1.intValue();
i2 = b2.intValue();
String str1 = "int value of Byte " + b1 + " is " + i1;
String str2 = "int value of Byte " + b2 + " is " + i2;
System.out.println( str1 );
System.out.println( str2 );
}
}
输出结果int value of Byte 1 is 1
int value of Byte -1 is -1
示例
现在让我们来看另一个示例-import java.lang.*;
public class Demo {
public static void main(String[] args){
Byte b1, b2;
String s1, s2;
b1 = new Byte("-123");
b2 = new Byte("0");
s1 = b1.toString();
s2 = b2.toString();
String str1 = "String value of Byte " + b1 + " is " + s1;
String str2 = "String value of Byte " + b2 + " is " + s2;
System.out.println( str1 );
System.out.println( str2 );
}
}
输出结果String value of Byte -123 is -123
String value of Byte 0 is 0