LAST UPDATED: NOVEMBER 24, 2020
Java Long valueOf(long i) Method
Java valueOf(long i)
method is part of the Long
class of the java.lang
package. This method is used to return the equivalent Long object of the long premitive value passed as an argument.
Syntax:
public static Long valueOf(long i)
Parameters:
The parameter passed is the long value whose equivalent Long object is to be returned.
Returns:
Returns the Long object of the long value passed as a parameter.
Example 1:
Here, the Long object representations are returned of the long value passed.
import java.lang.Long;
public class StudyTonight
{
public static void main(String[] args)
{
System.out.println("Equivalent Long object Value = " + Long.valueOf(211L));//returns a Long object representing the long specified
System.out.println("Equivalent Long object Value = " + Long.valueOf(-533L));//returns a Long object representing the long specified
}
}
Equivalent Long object Value = 211
Equivalent Long object Value = -533
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.lang.Integer;
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the value");
Scanner sc=new Scanner(System.in);
long x = sc.nextLong();
System.out.println("Equivalent Long object Value = " + Long.valueOf(x));//returns a Long object representing the long specified
}
catch(Exception e)
{
System.out.println("Invalid input!!");
}
}
}
Enter the value
688
Equivalent Long object Value = 688
*********************************************
Enter the value
-668
Equivalent Long object Value = -668
*********************************************
Enter the value
0x5556
Invalid input!!
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.