Java provides multiple methods for string-to-long conversion, each with distinct functionalities. The primary method, Long.parseLong()
, is a straightforward and widely used approach to convert a string containing numeric characters into a long
primitive data type. It efficiently parses the string representation of a long value into its numeric form.
On the other hand, Long.valueOf()
returns an object of the Long
class, allowing further object-oriented manipulations or interactions. This method provides flexibility, especially when working within object-based paradigms or requiring object-specific functionalities for long values.
Moreover, NumberFormat
offers the capability of localized parsing, accommodating diverse locales and formats for long values within strings. This method proves advantageous when dealing with numeric data across different regions or when adherence to specific formatting rules is necessary.
1. Convert String to long using Long.parseLong
Long.parseLong()
is a core Java method utilized for converting string representations of numeric values into primitive long
data types. It takes a string argument that contains a valid representation of a long value and returns the corresponding long
primitive type.
Long.parseLong()
performs this conversion by interpreting the string contents as a numeric value, handling positive or negative numbers, and recognizing valid numeric characters. However, it might throw a NumberFormatException
if the string does not represent a valid long value or contains non-numeric characters.
String str = "12345"; long longValue = Long.parseLong(str); System.out.println("Using parseLong(): " + longValue); //Next Example String str2 = "-98765"; long longValue2 = Long.parseLong(str2); System.out.println("Using parseLong(): " + longValue2);
2. Method – convert String to Long in java (example)
Long.valueOf()
is a method in Java used to convert strings representing numeric values into Long
objects. Unlike Long.parseLong()
that directly returns a primitive long
, Long.valueOf()
returns an object of the Long
class, which is a wrapper for the long
primitive type.
The returned Long
object can be utilized in an object-oriented context, enabling functionalities such as interaction with collections, utilization of object-specific methods (e.g., compareTo()
, equals()
, toString()
), and compatibility with object-based paradigms. Additionally, enables to use in collections like Hashmap as a Key or Value.
String str = "12345"; Long longObject = Long.valueOf(str); System.out.println("Using valueOf(): " + longObject); //Next Example String str4 = "0"; Long longObject2 = Long.valueOf(str4); System.out.println("Using valueOf(): " + longObject2);
3. Method – convert String to long in java (example)
Java’s Scanner
class provides the facility to structured input, including numeric values, from strings. By utilizing the nextLong()
method of the Scanner
class, we can extract long
values from strings with various formatting or patterns.
The Scanner
class tokenizes the input string based on specific delimiters, allowing extraction of long
values or other data types according to defined patterns. Scanner
class supports operations on input streams, enabling parsing of numeric values not only from strings but also from other sources such as files or user inputs via the console.
String str = "12345"; Scanner scanner = new Scanner(str); long longValue = scanner.nextLong(); scanner.close(); //Next Example String str3 = "The number is: 98765"; Scanner scanner = new Scanner(str3); long longValue3 = scanner.nextLong(); scanner.close(); System.out.println("Using Scanner: " + longValue3); //Next Example String str6 = "A different number: -54321"; Scanner scanner2 = new Scanner(str6); long longValue4 = scanner2.nextLong(); scanner2.close(); System.out.println("Using Scanner: " + longValue4);
Summary: String to Long/long:
there are various methods to perform String to long/Long conversion and each solve particular purpose. We can use these methods to convert String to long/Long data types.