Java.lang Package Classes

Java Long - valueOf() Method



The java.lang.Long.valueOf() method returns a Long instance representing the specified long value. If a new Long instance is not required, this method should generally be used in preference to the constructor Long(long), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Syntax

public static Long valueOf(long l)

Parameters

l Specify a long value.

Return Value

Returns a Long instance representing l.

Exception

NA.

Example:

In the example below, the java.lang.Long.valueOf() method returns a Long instance representing the given long value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating long value
    long x = 25;

    //creating Long instance
    Long y = Long.valueOf(x);

    //printing the long value 
    System.out.println("The long value is: " + x); 

    //printing the Long instance 
    System.out.println("The Long instance is: " + y);   
  }
}

The output of the above code will be:

The long value is: 25
The Long instance is: 25

❮ Java.lang - Long

5