Java中的字节类字段示例
Byte 类是原始类型 byte 的包装类,它包含几种有效处理字节值的方法,例如将其转换为字符串表示形式,反之亦然。 Byte 类的对象可以保存单个字节值。
Byte 类以Fields的形式提供了四个常量。这些都是:
- MAX_VALUE: MAX_VALUE是 Byte 类的实例变量,用于返回最大字节值。
句法:
public static final byte MAX_VALUE
用法:
Byte.MAX_VALUE
返回值:它返回一个等于 127 的字节值。
下面是 MAX_VALUE 的实现:
// Java code to implement // MAX_VALUE of Byte class class GFG { public static void main(String[] args) { // byte variable byte max_value; // MAX_VALUE Byte class max_value = Byte.MAX_VALUE; // printing the MAX_VALUE System.out.println(max_value); } }
输出:127
- MIN_VALUE: MIN_VALUE是 Byte 类的实例变量,用于返回最小字节值。
句法:
public static final byte MIN_VALUE
用法:
Byte.MIN_VALUE
返回值:它返回一个等于-128 的字节值。
下面是 MIN_VALUE 的实现:
// Java code to implement // MIN_VALUE of Byte class class GFG { public static void main(String[] args) { // byte variable byte min_value; // MIN_VALUE Byte class min_value = Byte.MIN_VALUE; // printing the MIN_VALUE System.out.println(min_value); } }
输出:-128
- SIZE: SIZE是 Byte 类的实例变量,用于返回以二进制表示(二进制补码)表示字节值所需的位数。
句法:
public static final int SIZE
用法:
Byte.SIZE
返回值:它返回一个等于 8 的 int 值。
下面是 SIZE 的实现:
// Java code to implement // SIZE of Byte class class GFG { public static void main(String[] args) { // SIZE Byte class int output = Byte.SIZE; // printing the output System.out.println(output); } }
输出:8
- TYPE: TYPE是 Byte 类的实例变量,用于返回表示原始数据类型字节的 Class 实例。
句法:
public static final Class
TYPE 用法:
Byte.TYPE
返回值:它返回一个表示原始数据类型字节的 Class 实例。
下面是TYPE的实现:
// Java code to implement // TYPE of Byte class class GFG { public static void main(String[] args) { // TYPE variable of Byte class Class
output = Byte.TYPE; // printing the output System.out.println(output); } } 输出:byte