How to convert a string to long in Java is one of those frequently asked questions by a beginner who has started learning Java programming language and not aware of how to convert from one data type to another. Converting String to long is similar to converting String to Integer in Java, in-fact if you know how to convert String to Integer than you can convert String to Long by following same procedure. Though you need to remember few things while dealing with long and String first of all long is primitive type which is wrapped by Long wrapper class and String is an Object in Java backed by character array. Before Java 5 you need to take an extra step to convert Long object into long primitive but after introduction of autoboxing and unboxing in Java 5, Java language will perform that conversion for you. This article is in continuation of our previous conversion tutorial like how to convert String to Double in Java or how to convert String to Enum in Java. In this Java tutorial we will learn 3 different ways to convert String to long by code example and we will also analyze pros and cons of each approach.
How to convert String to long in Java
Here are 3 different ways to convert an String to long primitive value in Java:
- Using Long.valueOf() method
- Using Long.parseLong() method
- Using java.lang.Long constructor
- Using Long decode() method
String to long - Long.valueOf
Long.valueOf() is static factory method from java.lang.Long class which accepts an String argument and returns an equivalent long primitive value. Long.valueOf() will throw NumberFormatException if String is not convertible into long due to any reason like String contains non numeric value, numeric value of String is beyond MAX and MIN value of Long etc. Though negative sign can be included to denote negative long value as first character. Main advantage of valueOf() is that its an static factory method and can cache frequently used long value, since long is an immutable object its safe to reuse cached long values. valueOf() is also an overloaded method which accepts radix as second argument to convert hexadecimal or octal String to long. Here is an example of converting String to long value using valueOf method in Java:
As per caching JDK's Long.valueOf() method caches values upto -128 to 127 and returns same Long object which will return true if compared with equality operator "==". you can also look code of valueOf() method on java.lang.Long class to verify it.
String to long - Long.parseLong
Long.parseLong() is another static method from java.lang.Long class which converts String into long value. parseLong() also throws java.lang.NumberFormatException if provided String value is not convertible into long and also provides addition overloaded method which accepts radix to convert octal and hexadecimal long values in String format. Most of the String to long conversion uses parseLong for conversion part, in fact Long.valueOf() also calls parseLong method to convert long to String. parseLong() also accepts negative sign as first character in string input. Though "l" and "L" values are not permitted inside String input and throw NumberFormatException.
Long.decode() and Long Constructor
Other two methods using a java.lang.Long constructor which accepts String and Long.decode() also work in similar fashion. They also throw NumberFormatException if String is not convertible into long. Long.decode() is good in terms it accepts "#" as 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 hexadecimal long String value into long primitive number:
long number = Long.decode("#FFFF"); //65535 in decimal
see code example section for more examples of decode() method of Long class.
Code Example - String to long conversion in Java
Here is complete code example of all four ways to convert String to long in Java discussed in this Java tutorial.
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 rest of time to convert String to long.
Other Java programming tutorials for beginners from Javarevisited Blog