Introduction
Converting a string to a long is a common task in Java programming. It allows us to take a numeric value represented as a string and convert it into a long integer that we can use for calculations or comparisons. T

This article will explain the different methods to accomplish this conversion. We'll learn this conversion using different methods like the parseLong() method, the valueOf() method, and the constructor of the Long class.
Methods to Convert String to Long
Using the parseLong() method of the Long class:
The parseLong() method is a convenient way to convert a string to a long. It is a static method provided by the Long class. Here's an example of how to use it:
Syntax :
long parseLong(String s)
Example :
String str = "9876543210";
try {
long num = Long.parseLong(str);
System.out.println("Converted long: " + num);
} catch (NumberFormatException e) {
System.out.println("Invalid string format for long: " + str);
}
In this example:
- We have a string "9876543210" stored in the variable str.
- We use a try-catch block to handle any potential NumberFormatException that may occur during the conversion process.
- Inside the try block, we pass the string str to the Long.parseLong() method. This method parses the string and returns the corresponding primitive long value. We assign this value to the variable num.
- Finally, we print the converted long value using System.out.println().
- If the string cannot be parsed as a long (e.g., if it contains non-numeric characters), a NumberFormatException will be thrown. The catch block will catch this exception and print an appropriate error message, indicating that the string format is invalid for a long.
The parseLong() method provides a direct way to convert a string to a primitive long value without the need for creating a Long object explicitly.
If the string cannot be parsed as a long (e.g., if it contains non-numeric characters), a NumberFormatException will be thrown. The catch block will catch this exception and print an appropriate error message, indicating that the string format is invalid for a long.
Using valueOf() method of Long class
The valueOf() method is a static method in the Long class that takes a string as input and returns a Long object representing the string's value. It is used to convert a string to a Long object.
Here's the syntax of the valueOf() method:
public static Long valueOf(String s) throws NumberFormatException
The valueOf() method takes a string parameter `s` and returns a Long object. If the string cannot be parsed as a Long, it throws a NumberFormatException.
For example :
String str = "1234567890";
try {
Long longObj = Long.valueOf(str);
long num = longObj.longValue();
System.out.println("Converted long: " + num);
} catch (NumberFormatException e) {
System.out.println("Invalid string format for Long: " + str);
}
In this example:
1. We have a string "1234567890" stored in the variable `str`.
2. We use a try-catch block to handle any potential NumberFormatException that may occur during the conversion process.
3. Inside the try block, we pass the string `str` to the `Long.valueOf()` method. This method parses the string and returns a Long object representing the string's value. We assign this Long object to the variable `longObj`.
4. To get the primitive long value from the Long object, we call the `longValue()` method on `longObj` and assign the result to the variable `num`.
5. Finally, we print the converted long value using `System.out.println()`.
6. If the string cannot be parsed as a Long (e.g., if it contains non-numeric characters), a NumberFormatException will be thrown. The catch block will catch this exception and print an appropriate error message, indicating that the string format is invalid for a Long.
The valueOf() method provides a convenient way to convert a string to a Long object, which can be useful when working with the Long class and its methods.
Using the constructor of the Long class
The Long class provides a constructor that accepts a string as a parameter and creates a Long object representing the string's value.
Syntax
public Long(String s) throws NumberFormatException
The constructor takes a string parameter `s` and creates a new Long object. If the string cannot be parsed as a Long, it throws a NumberFormatException.
Example
String str = "5678901234";
try {
Long longObj = new Long(str);
long num = longObj.longValue();
System.out.println("Converted long: " + num);
} catch (NumberFormatException e) {
System.out.println("Invalid string format for Long: " + str);
}
In this example:
1. We have a string "5678901234" stored in the variable `str`.
2. We use a try-catch block to handle any potential NumberFormatException that may occur during the object creation process.
3. Inside the try block, we create a new instance of the Long class by passing the string `str` to its constructor. This creates a new Long object `longObj` representing the string's value.
4. To get the primitive long value from the Long object, we call the `longValue()` method on `longObj` and assign the result to the variable `num`.
5. Finally, we print the converted long value using `System.out.println()`.
6. If the string cannot be parsed as a Long (e.g., if it contains non-numeric characters), a NumberFormatException will be thrown. The catch block will catch this exception and print an appropriate error message, indicating that the string format is invalid for a Long.