Java Number byteValue()方法与示例
数字类byteValue()方法
byteValue()方法在java.lang包中可用。
byteValue()方法用于返回此Number对象表示的值(转换为字节类型)(通过强制转换)。
byteValue()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。
从Number转换为字节时,byteValue()方法不会引发异常。
语法:
public byte byteValue();
参数:
它不接受任何参数。
返回值:
该方法的返回类型为字节,它返回此Number对象表示的转换后的值(从类型Number到字节)。
示例
//Java程序演示示例
//byteValue()Number类的方法
public class ByteValueOfNumberClass {
public static void main(String[] args) {
//变量初始化
int i1 = 10;
float f1 = 20.62f;
//它返回此Number对象表示的字节值
//转换为字节
Integer i = new Integer(i1);
//显示我的结果
System.out.println("i.byteValue(): " + i.byteValue());
//它返回此Number对象表示的字节值
//转换为字节
Float f = new Float(f1);
//显示结果
System.out.println("f.byteValue(): " + f.byteValue());
}
}输出结果
i.byteValue(): 10 f.byteValue(): 20