- 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
Java - Number longValue() Method
The Java Number longValue() method is used to convert the value of the specified number into a long value.
The long data type is Java’s primitive data type and is a 64-bit two's complement integer, similar to the int data type. However, the signed long has a higher range than the int data type; with its minimum value being -263 and maximum value being 263-1. From Java SE 8+, the long data type can also be used to represent an unsigned 64-bit long value, which has a minimum value of 0 and a maximum value of 264-1.
The default value and size of long data type is 0 and 8 bytes.
Note − This conversion may involve rounding or truncation of the given number.
Syntax
Following is the syntax for Java Number longValue() method
Parameters
This method does not accept any parameters.
Return Value
This method returns the long value after conversion.
Example
The following example shows the usage of Java Number longValue() method. We assign an integer value to a Integer class reference variable implicitly. We invoke the method on this object created and convert the value to long data type.
public class NumberLongVal { public static void main(String[] args) { // get a number as int Integer x = 4672; // print their value as long System.out.println("x as int: " + x + ", x as long: " + x.longValue()); } }
Let us compile and run the program given, the output obtained and displayed is as follows −
x as int: 4672, x as long: 4672
Example
In the following, we a float value is assigned to a Float class reference variable. The method is called on this object created and the float value is converted to a long value as a result.
public class NumberLongVal { public static void main(String[] args) { // get a number as float Float x = new Float(123456f); // print their value as long System.out.println("x as float: " + x + ", x as long: " + x.longValue()); } }
Let us compile and run the above program, this will produce the following result −
x as float:123456.0, x as long:123456
Example
In the example below, a double value is assigned to a Double class reference variable. The method will be called on the Double object created to convert the double value to long value.
public class NumberLongVal { public static void main(String[] args) { // get a number as double Double x = 9876d; // print their value as long System.out.println("x as double: " + x + ", x as long: " + x.longValue()); } }
Let us compile and run the above program, this will produce the following result −
x as double: 9876.0, x as long: 9876
Example
In the following example, a Byte class reference variable will have a valid byte value assigned to it. The method is called on the Byte object to convert the byte value to its corresponding long value.
public class NumberLongVal { public static void main(String[] args) { // get a number as int Byte x = -56; // print their value as long System.out.println("x as byte: " + x + ", x as long: " + x.longValue()); } }
The output for the program given above is obtained as follows −
x as byte: -56, x as long: -56
To Continue Learning Please Login
Login with Google