这个 JAVA朗,加倍。字节值() 是Java中的一个内置方法,它以字节的形式返回Double的值(通过强制转换为字节)。基本上,它用于将双精度类型的原语转换缩小为字节值。
null
语法:
public byte byteValue()
参数: 该函数不接受任何参数。
返回值: 此方法返回由转换为类型的对象表示的双精度值 字节 .
例如:
Input : 12 Output : 12 Input : 1023 Output : -1
下面的程序演示了java的使用。朗,加倍。字节值()函数:
项目1:
// Program to illustrate the Double.byteValue() method import java.lang.*; public class GFG { public static void main(String[] args) { Double value = 1023d; // Returns the value of Double as a byte byte byteValue = value.byteValue(); System.out.println( "Byte Value of num = " + byteValue); // Another example value = 12d; byteValue = value.byteValue(); System.out.println( "Byte Value of num = " + byteValue); } } |
输出:
Byte Value of num = -1 Byte Value of num = 12
项目2: 演示负数的字节值。
// Java code to illustrate java.lang.Double.byteValue() method import java.lang.*; public class GFG { public static void main(String[] args) { Double value = -1023d; // Returns the value of Double as a byte byte byteValue = value.byteValue(); System.out.println( "Byte Value of num = " + byteValue); // Another example value = -12d; byteValue = value.byteValue(); System.out.println( "Byte Value of num = " + byteValue); } } |
输出:
Byte Value of num = 1 Byte Value of num = -12
方案3: 在参数中传递十进制值时。
// Program to illustrate java.lang.Double.byteValue() method import java.lang.*; public class GFG { public static void main(String[] args) { Double value = 11.24 ; // Returns the value of Double as a byte byte byteValue = value.byteValue(); System.out.println( "Byte Value of num = " + byteValue); // Another example value = 6.0 ; byteValue = value.byteValue(); System.out.println( "Byte Value of num = " + byteValue); } } |
输出:
Byte Value of num = 11 Byte Value of num = 6
方案4: 当字符串值作为参数传递时。
// Code to illustrate Double.byteValue() import java.lang.*; public class GFG { public static void main(String[] args) { Double value = "45" ; // Returns the value of Double as a byte byte byteValue = value.byteValue(); System.out.println( "Byte Value of num = " + byteValue); } } |
编译错误:
prog.java:9: error: incompatible types: String cannot be converted to Double Double value = "45"; ^ 1 error
参考 : https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#byteValue–
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END