
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java - Short longValue() Method
The Java Short longValue() method retrieves the equivalent long value of the given short value after a widening primitive conversion which means conversion of a lower data type to a higher data type. The data type long can store whole numbers in the range of -2,147,483,648 to 2,147,483,647.
For example, assume we have a short value ’687’. The equivalent long value of the given short value is ‘687’.
Short s1 = -7980 Long l = -7980 // corresponding long value
Syntax
Following is the syntax for Java Short longValue() method −
public long longValue()
Parameters
This method does not accept any parameter.
Return Value
This method returns the numeric value represented by this object after conversion to type long.
Example
Following is an example to show the usage of Java Short longValue() method. Here we are creating a Short object 'obj' and with a positive value and trying to print its long value −
import java.lang.*; 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); } }
Output
Let us compile and run the above program, this will produce the following result −
Value = 5432 Value = 30
Example
In the following example the three short values i.e. ‘value1’, ‘value2’ and ‘value3’ are converted to their equivalent long values using the Java Short longValue() method. Then the result of subtraction and multiplication operations on these values is displayed.
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); } }
Output
Following is an output of the above code −
Difference = 678-0-98 = 776 Product = 678*0*98 = 0
Example
In the example given below a negative short value is assigned to the reference of the Short class. Thereafter, it's equivalent long value is retrieved by invoking the longValue() method on this Short object.
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()); } }
Output
On executing the program above, the output is obtained as follows −
The corresponding long value for 655 = 655 The corresponding long value for -6578 = -6578
Example
In the example below short variables ‘value1’ and ‘value2’ are created. Then the MAX_VALUE and MIN_VALUE is assigned to these variables. Thereafter, we retrieve its corresponding long value.
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); } }
Output
While executing the above code, we get the following output −
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