Java.lang Package Classes

Java Long - valueOf() Method



The java.lang.Long.valueOf() method returns a Long object holding the value of the specified String. The argument is interpreted as representing a signed decimal long, exactly as if the argument were given to the parseLong(java.lang.String) method. The result is a Long object that represents the integer value specified by the string.

In other words, this method returns a Long object equal to the value of: new Long(Long.parseLong(s)).

Syntax

public static Long valueOf(String s)
                    throws NumberFormatException

Parameters

s Specify the string to be parsed.

Return Value

Returns a Long object holding the value represented by the String argument.

Exception

Throws NumberFormatException, if the string cannot be parsed as a long.

Example:

In the example below, the java.lang.Long.valueOf() method returns a Long object holding the value given by the specified String.

import java.lang.*;

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

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

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

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

The output of the above code will be:

The string is: 25
The Long object is: 25

❮ Java.lang - Long