Java中的字节类
Byte类将原始类型byte的值包装在对象中。字节类型的对象包含一个类型为字节的字段。
以下是Byte类的一些方法-
byteValue()此方法以字节形式返回此Byte的值。
此方法以数字方式比较两个Byte对象。
此方法将字符串解码为字节。
doubleValue()此方法以double形式返回此Byte的值。
此方法将此对象与指定对象进行比较。
floatValue()此方法以浮点数形式返回此Byte的值。
hashCode()此方法为此字节返回一个哈希码。
intValue()此方法以int形式返回此Byte的值。
longValue()此方法返回此Byte的long值。
此方法将字符串参数解析为带符号的十进制字节。
现在让我们看一个例子-
示例
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