Java - Long Class valueOf() Method

Long class valueOf() method: Here, we are going to learn about the valueOf() method of Long class with its syntax and example. By Preeti Jain Last updated : March 20, 2024

Syntax

    public static Long valueOf (long value);
    public static Long valueOf (String value);
    public static Long valueOf (String value, int radix's);

Long class valueOf() method

  • valueOf() method is available in java.lang package.
  • valueOf (long value) method is used to represent a Long object denoted by the given argument (value) is of long type.
  • valueOf (String value) method is used to represent a Long object holding the long value denoted by the given argument (value) is of String type.
  • valueOf (String value, int radix's) method is used to represent a Long object holding the long value of the given argument (value) in the radix's given by the second argument.
  • valueOf (long value) method does not throw an exception at the time of returning a Long instance.
  • Similarly, valueOf (String value), valueOf (String value, int radix's) method may throw a NumberFormatException at the time of returning an instance.
    NumberFormatException: In this exception, if String does not contain the parsable number.
  • These are static methods, they are accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.

Parameters

  • In the first case, long value – represents the value of long type.
  • In the second case, String value – represents the value of String type.
  • In the third case, String value, int radix'svalue represents the value of String type in the radix's given by the second parameter.

Return Value

In the first case, the return type of this method is Long - it returns the Long representation of this long argument.

In the second case, the return type of this method is Long - it returns the Long representation of this String argument.

In the third case, the return type of this method is Long - it returns the Long representation of the String argument in the radix’s given by the second argument.

Example

// Java program to demonstrate the example 
// of valueOf() method of Long class

public class ValueOfLongClass {
    public static void main(String[] args) {
        // Object initialization
        Long ob1 = new Long(100);
        Long ob2 = new Long(200);
        Long ob3 = new Long(40);

        // Display ob1,ob2,ob3 values
        System.out.println("ob1: " + ob1);
        System.out.println("ob2: " + ob2);
        System.out.println("ob3: " + ob3);

        // It returns Long object holding the value 
        // denoted by the given long argument
        Long value1 = ob1.valueOf(20);

        // String object initialization for valueOf(String s)
        String s = "80";

        // It returns Long object holding the value 
        // denoted by the given String argument
        Long value2 = ob2.valueOf(s);

        // It returns Long object holding the value 
        // denoted by the given String argument with radix 20
        Long value3 = ob3.valueOf(s, 20);

        // Display result values
        System.out.println("ob1.valueOf(20): " + value1);
        System.out.println("ob2.valueOf(s): " + value2);
        System.out.println("ob3.valueOf(s,20): " + value3);
    }
}

Output

ob1: 100
ob2: 200
ob3: 40
ob1.valueOf(20): 20
ob2.valueOf(s): 80
ob3.valueOf(s,20): 160

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.