Java - Short longValue() 方法



Java Short longValue() 方法检索给定 short 值的等效 long 值,该值经过宽化原始类型转换后获得,这意味着将较低数据类型转换为较高数据类型。long 数据类型可以存储 -2,147,483,648 到 2,147,483,647 范围内的整数。

例如,假设我们有一个 short 值“687”。给定 short 值的等效 long 值为“687”。

Short s1 = -7980 Long l = -7980 // corresponding long value

语法

以下是Java Short longValue()方法的语法:

public long longValue()

参数

此方法不接受任何参数。

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

返回值

此方法在转换为 long 类型后返回此对象表示的数值。

从具有正值的 Short 获取 long 值示例

以下是一个示例,用于演示 Java Short longValue() 方法的用法。在这里,我们创建一个具有正值的 Short 对象“obj”,并尝试打印其 long 值:

Open Compiler
package com.tutorialspoint; public class ShortDemo { public static void main(String[] args) { /* * returns the short value represented by the short object * converted to type long */ Short obj = new Short("5432"); long l = obj.longValue(); System.out.println("Value = " + l); obj = new Short("30"); l = obj.longValue(); System.out.println("Value = " + l); } }

输出

让我们编译并运行上面的程序,这将产生以下结果:

Value = 5432
Value = 30

从 Short 对象获取 long 值示例

在下面的示例中,三个 short 值,即“value1”、“value2”和“value3”,使用 Java Short longValue() 方法转换为其等效的 long 值。然后显示这些值减法和乘法运算的结果。

Open Compiler
package com.tutorialspoint; import java.util.Scanner; public class ShortDemo { public static void main(String[] args) { Short value1 = 678; Short value2 = 0; Short value3 = 98; // subtracting long values Long l1 = value1.longValue() + value2.longValue() + value3.longValue(); System.out.println("Difference = " + value1.longValue() + "-" + value2.longValue() + "-" + value3.longValue() + " = " + l1); // multiplying long values Long l2 = value1.longValue() * value2.longValue() * value3.longValue(); System.out.println("Product = " + value1.longValue() + "*" + value2.longValue() + "*" + value3.longValue() + " = " + l2); } }

输出

以下是上述代码的输出:

Difference = 678-0-98 = 776
Product = 678*0*98 = 0

从 Short 对象获取 long 值示例

在下面给出的示例中,一个负的 short 值被赋值给 Short 类的引用。然后,通过在此 Short 对象上调用 longValue() 方法来检索其等效的 long 值。

Open Compiler
package com.tutorialspoint; public class ShortDemo { public static void main(String[] args) { short value1 = 655; Short value2 = new Short(value1); // returning the long value of the short object Long l1 = value2.longValue(); System.out.println("The corresponding long value for " + value1 + " = " + l1); // passing a negative integer Short value3 = new Short("-6578"); System.out.println("The corresponding long value for " + value3 + " = " + value3.longValue()); } }

输出

执行上述程序后,获得的输出如下:

The corresponding long value for 655 = 655
The corresponding long value for -6578 = -6578

从具有最小值和最大值的 Short 获取 long 值示例

在下面的示例中,创建了 short 变量“value1”和“value2”。然后将 MAX_VALUE 和 MIN_VALUE 分配给这些变量。此后,我们检索其对应的 long 值。

Open Compiler
package com.tutorialspoint; public class ShortDemo { public static void main(String[] args) { Short value1 = Short.MIN_VALUE; Short value2 = Short.MAX_VALUE; System.out.println("The short MIN_VALUE is: " + value1); System.out.println("The short MAX_VALUE is: " + value2); // it returns long type value long result1 = value1.longValue(); long result2 = value2.longValue(); System.out.println("The long minimum value of short is: " + result1); System.out.println("The long maximum value of short is: " + result2); } }

输出

执行上述代码时,我们将得到以下输出:

The short MIN_VALUE is: -32768
The short MAX_VALUE is: 32767
The long minimum value of short is: -32768
The long maximum value of short is: 32767
java_lang_short.htm
广告