Home »
Java programming language
Java - Long Class parseLong() Method
Long class parseLong() method: Here, we are going to learn about the parseLong() method of Long class with its syntax and example.
By Preeti Jain Last updated : March 20, 2024
Syntax
public static long parseLong(String str);
public static long parseLong(String str, int radix's);
Long class parseLong() method
- parseLong() method is available in java.lang package.
- parseLong(String str) method is used to return the long value corresponding to the given String denotation or in other words we can say this method is used to convert string value to a long value.
- parseLong (String str, int radix's) method is used to return the long value corresponding to the given String denotation as a signed long in the radix's given by the second argument.
- parseLong (String str), parseLong (String str, int radix's) method may throw a NumberFormatException at the time of conversion from String to long.
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, String value – represents the value of String type.
- In the second case, String value, int radix's – value 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 String argument.
In the second 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 parseLong() method of Long class
public class ParseShortOfLongClass {
public static void main(String[] args) {
// Variables initialization
String str1 = "100";
String str2 = "67";
int radix = 20;
// Object initialization
Long l1 = new Long(str2);
// It convert string into long by calling parseLong(str1) method
// and store the result in another variable of long type
long result = l1.parseLong(str1);
// Display result
System.out.println("l1.parseLong(str1): " + result);
// It convert string into long with radix 20 by
// calling parseLong(str1,radix's) method
// and store the result in a variable of long type
result = l1.parseLong(str1, radix);
// Display result
System.out.println("l1.parseLong(str1,radix): " + result);
}
}
Output
l1.parseLong(str1): 100
l1.parseLong(str1,radix): 400