4 Ways to Convert String to long in Java? Example Tutorial
Before Java 5 you need to take an extra step to convert a Long object into a long primitive but after the introduction of autoboxing and unboxing in Java 5, Java language will perform that conversion for you.
How to convert String to long in Java?
Here are 4 different ways to convert a String to a long primitive value in Java:
- Using Long.valueOf() method
- Using Long.parseLong() method
- Using java.lang.Long constructor
- Using Long decode() method
1. String to long in Java – Long.valueOf Example
Though negative sign can be included to denote negative long value as the first character. The main advantage of valueOf() is that it’s a static factory method and can cache frequently used long values, since long is an immutable object it’s safe to reuse cached long values.
The valueOf() is also an overloaded method that accepts radix as the second argument to convert hexadecimal or octal String to long.
Here is an example of converting String to long value using the valueOf method in Java:
long decimalNumber = Long.valueOf(“55555”);
long octalNumber = Long.valueOf(“55555”, 8);
long hexNumber = Long.valueOf(“5555”, 16);
2. String to long in Java – Long.parseLong Example
Most of the String to long conversion uses parseLong for the conversion part, in fact, Long.valueOf() also calls the parseLong method to convert long to String.
parseLong() also accepts negative sign as the first character in string input.
Though “l” and “L” values are not permitted inside String input and throw NumberFormatException.
long negativeLong = Long.parseLong(“-1024”); //represent negative long value
long incorrectLong = Long.parseLong(“-1024L”); // “L” is not permitted result in NumberFormatException
3. Long.decode() and Long Constructor
They also throw NumberFormatException if String is not convertible into long. Long.decode() is good in terms it accepts “#” as an indication of hexadecimal long values.
It also accept “0” for octal long numbers, “0x” and “0X” for hexadecimal long numbers. Here is an example of using Long.decode() method to convert a hexadecimal long String value into a long primitive number:
see code example section for more examples of decode() method of the Long class.
4. Code Example – String to long conversion in Java
Here is a complete code example of all four ways to convert String to long in Java discussed in this Java tutorial.
* Java program to convert String to long in Java. In this program, we will see 4 examples
* to convert String to long primitive in Java.
* @author Javin Paul
*/
public class StringToLong {
public static void main(String args[]) {
String strLong = “2323232”;
//Convert String to long using Long.valueOf(),better because
//it can cache frequently used long values
long number = Long.valueOf(strLong);
System.out.println(“String to long conversion using valueOf :” + number);
//Convert String to long in Java using java.lang.Long object
strLong =“4444444444444444444”;
Long numberObject = new Long(strLong);
number = numberObject; //auto boxing will take care of long to Long conversion
System.out.println(“String to long conversion example using Long object: “ + number);
//Convert String to long with Long.parseLong method in Java
strLong=“999999999999999999”;
Long parsedLong = Long.parseLong(strLong) ;
number = parsedLong; //auto-boxing will convert Long object to primitive long
System.out.println(“String to long conversion using parseLong() method: “ + number);
//Convert String to long using Long.decode() method in Java
strLong =“-1023454”;
number = Long.decode(strLong);
System.out.println(“String to long conversion using decode() method: “ + number);
//String to long in hexadecimal format
strLong =“0xFFFF”;
number = Long.decode(strLong);
System.out.println(“String to long example in hex format decode() method: “ + number);
//String to long in octal format
strLong =“0777”;
number = Long.decode(strLong);
System.out.println(“String to long example in octal format decode() method: “ + number);
}
}
Output:
String to long conversion using valueOf :2323232
String to long conversion example using Long object: 4444444444444444444
String to long conversion using parseLong() method: 999999999999999999
String to long conversion using decode() method: –1023454
String to long example in hex format decode() method: 65535
String to long example in octal format decode() method: 511
That’s all on How to convert String to long in Java. We have seen four ways to change String values to long e.g. Long.valueOf(), Long.parseLong() and Long.decode() method along with classic constructor approach for converting String to long in Java.
I personally prefer Long.valueOf() most of the time and Long.parseLong() in the rest of time to convert String to long.
Other Java programming tutorials for beginners from Javarevisited Blog
And, lastly one question for you? Which one is your favorite way to convert String to long in Java? Long.parseLong() or valueOf()? Can you also use these way to convert String to Long object in Java? If not why not?