Java 中的 Byte 类
javaobject oriented programmingprogramming更新于 2024/6/4 7:03:00
Byte 类将原始类型 byte 的值包装在对象中。Byte 类型的对象包含一个类型为 byte 的字段。
以下是 Byte 类的一些方法 −
Sr.No. | 方法 &说明 |
---|---|
1 | byte byteValue() 此方法以字节形式返回此 Byte 的值。 |
2 | int compareTo(Byte anotherByte) 此方法对两个 Byte 对象进行数值比较。 |
3 | static Byte decrypt(String nm) 此方法将 String 解码为 Byte。 |
4 | double doubleValue() 此方法以双精度形式返回此字节的值。 |
5 | boolean equals(Object obj) 此方法将此对象与指定对象进行比较。 |
6 | float floatValue() 此方法以浮点形式返回此字节的值。 |
7 | int hashCode() 此方法返回此字节的哈希码。 |
8 | int intValue() 此方法以 int 形式返回此字节的值。 |
9 | long longValue() 此方法以 long 形式返回此字节的值。 |
10 | static byte 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