The Long.parseLong()
method in Java is used to convert a String
to a long
primitive. This method has multiple versions to handle different use cases, including converting a string in a specified radix (base).
Table of Contents
- Introduction
parseLong()
Method Syntax- Overloaded Versions
parseLong(String s)
parseLong(String s, int radix)
- Examples
- Converting a Decimal String to
long
- Converting a String with a Radix to
long
- Handling
NumberFormatException
- Converting a Decimal String to
- Real-World Use Case
- Conclusion
Introduction
The Long.parseLong()
method is a static method in the Long
class in Java. It converts a string representation of a number to a long
primitive. This method is useful when you need to convert numeric strings into long integers for calculations or other operations.
parseLong()() Method Syntax
The Long.parseLong()
method has two overloaded versions:
1. Basic parseLong
Method
public static long parseLong(String s) throws NumberFormatException
- s: The string to be parsed.
- The method returns a
long
primitive representing the value of the specified string. - Throws
NumberFormatException
if the string does not contain a parsable long.
2. parseLong
Method with Radix
public static long parseLong(String s, int radix) throws NumberFormatException
- s: The string to be parsed.
- radix: The radix to be used while parsing (for example, 10 for decimal, 16 for hexadecimal).
- The method returns a
long
primitive representing the value of the specified string in the specified radix. - Throws
NumberFormatException
if the string does not contain a parsable long.
Examples
Converting a Decimal String to long
The parseLong()
method can be used to convert a decimal string to a long
.
Example
public class ParseLongExample {
public static void main(String[] args) {
String numberString = "123456789";
long number = Long.parseLong(numberString);
System.out.println("Parsed long: " + number);
}
}
Output:
Parsed long: 123456789
In this example, the method converts the string "123456789"
into the long 123456789
.
Converting a String with a Radix to long
The parseLong()
method can also be used to convert a string in a different numeral system (radix) to a long
.
Example
public class ParseLongWithRadixExample {
public static void main(String[] args) {
String hexString = "7FFFFFFFFFFFFFFF";
long number = Long.parseLong(hexString, 16);
System.out.println("Parsed long (hex): " + number);
}
}
Output:
Parsed long (hex): 9223372036854775807
In this example, the method converts the hexadecimal string "7FFFFFFFFFFFFFFF"
into the long 9223372036854775807
, which is the maximum value for a signed 64-bit integer.
Handling NumberFormatException
If the input string is not a valid representation of a long, the parseLong()
method throws a NumberFormatException
.
Example
public class ParseLongInvalidExample {
public static void main(String[] args) {
String invalidString = "abc";
try {
long number = Long.parseLong(invalidString);
System.out.println("Parsed long: " + number);
} catch (NumberFormatException e) {
System.out.println("Invalid string for parsing: " + invalidString);
}
}
}
Output:
Invalid string for parsing: abc
In this example, the method throws a NumberFormatException
because the string "abc"
is not a valid representation of a long.
Real-World Use Case
Parsing Configuration Values
In a real-world application, you might use the Long.parseLong()
method to parse configuration values from a properties file or environment variables.
Example
import java.util.Properties;
public class ConfigurationExample {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("maxConnections", "200");
properties.setProperty("timeout", "5000");
long maxConnections = Long.parseLong(properties.getProperty("maxConnections"));
long timeout = Long.parseLong(properties.getProperty("timeout"));
System.out.println("Max Connections: " + maxConnections);
System.out.println("Timeout: " + timeout);
}
}
Output:
Max Connections: 200
Timeout: 5000
In this example, the configuration values are parsed from a properties file and converted to long values using the Long.parseLong()
method.
Conclusion
The Long.parseLong()
method in Java is a powerful and useful tool for converting strings to long integers. By understanding how to use this method, you can efficiently handle tasks that involve parsing numeric strings in your Java applications. Whether you are dealing with decimal strings, strings in different numeral systems, or handling invalid inputs, the parseLong()
method provides a reliable solution for these tasks.
Comments
Post a Comment
Leave Comment