Keywords: Java | String Conversion | Long.parseLong
Abstract: This article provides an in-depth examination of various methods for converting strings to long integers in Java, with particular focus on the advantages and usage scenarios of the Long.parseLong() method. Through extensive code examples, it demonstrates different base conversions, exception handling, and performance optimization strategies, while comparing the differences between valueOf() method and deprecated constructors to offer comprehensive technical guidance for developers.
Core Methods for String to Long Conversion
In Java programming, conversion between strings and primitive data types is a common operation. When converting strings to long integers, the Long.parseLong() method is the most recommended approach. This method directly returns a long primitive type, avoiding unnecessary object creation and autoboxing overhead.
Detailed Analysis of Long.parseLong() Method
The Long.parseLong() method provides two overloaded forms: the single-parameter version defaults to decimal parsing, while the dual-parameter version allows specification of arbitrary bases. The following examples demonstrate usage in different scenarios:
// Decimal conversion examples
long result1 = Long.parseLong("473"); // Returns 473L
long result2 = Long.parseLong("-0"); // Returns 0L
// Different base conversion examples
long result3 = Long.parseLong("-FF", 16); // Hexadecimal, returns -255L
long result4 = Long.parseLong("1100110", 2); // Binary, returns 102L
long result5 = Long.parseLong("Hazelnut", 36); // Base-36, returns 1356099454469LException Handling Mechanism
When a string contains illegal characters or exceeds numerical range, Long.parseLong() throws NumberFormatException. Developers should properly handle these exceptional cases:
try {
long value = Long.parseLong("invalid");
} catch (NumberFormatException e) {
System.out.println("String format error: " + e.getMessage());
}Comparative Analysis of Alternative Methods
Besides Long.parseLong(), Java provides other conversion approaches, each with distinct advantages and disadvantages:
Long.valueOf() Method
Long.valueOf() returns a Long wrapper class object, requiring autounboxing to convert to primitive type:
String text = "999999999999";
Long wrapper = Long.valueOf(text); // Returns Long object
long primitive = wrapper.longValue(); // Unboxes to primitive type
// Or utilize autounboxing directly
long autoUnbox = Long.valueOf(text);Deprecated Constructors
Starting from Java 9, the Long(String) constructor has been marked as deprecated and should not be used in new code:
// Not recommended usage
@Deprecated
long deprecated = new Long("999999999");Performance Optimization Considerations
In performance-sensitive scenarios, Long.parseLong() demonstrates clear advantages over Long.valueOf():
- Avoids overhead of wrapper object creation
- Reduces garbage collection pressure
- Directly returns primitive type, eliminating unboxing operations
Practical Application Scenarios
String to long conversion finds applications in various scenarios:
// Configuration file reading
String configValue = "90210";
long numericValue = Long.parseLong(configValue);
// Mathematical operation verification
long doubled = numericValue + numericValue;
System.out.println("Original value: " + numericValue); // Outputs 90210
System.out.println("Doubled value: " + doubled); // Outputs 180420Array Conversion Strategies
When converting string arrays, each element must be processed individually through iteration:
String[] stringArray = {"100", "200", "300"};
long[] longArray = new long[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
longArray[i] = Long.parseLong(stringArray[i]);
}Best Practice Recommendations
Based on the above analysis, developers are advised to:
- Prioritize
Long.parseLong()for string to long conversion - Always handle potential
NumberFormatExceptionexceptions - Avoid using deprecated constructors
- Choose primitive type operations in performance-critical paths
- Appropriately select base parameters to meet different business requirements