Java.lang Package Classes

Java Long - valueOf() Method

The java.lang.Long.valueOf() method returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed long in the radix specified by the second argument, exactly as if the arguments were given to the parseLong(java.lang.String, int) method. The result is a Long object that represents the long value specified by the string.

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


public static Long valueOf(String s,
                           int radix)
                    throws NumberFormatException


s Specify the string to be parsed.
radix Specify the radix to be used in interpreting s.

Return Value

Returns a Long object holding the value represented by the string argument in the specified radix.


Throws NumberFormatException, if the String does not contain a parsable long.


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

import java.lang.*;

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

    //creating Long object using radix as 2 (binary)
    Long p = Long.valueOf(x, 2);

    //creating Long object using radix as 16 (hexadecimal)
    Long q = Long.valueOf(y, 16);

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

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

The output of the above code will be:

The string x is: 100
The string y is: 6F
The Long object p is: 4
The Long object q is: 111

❮ Java.lang - Long