Use Long.parseLong()

Copy Long.parseLong("0", 10)        // returns 0L
 Long.parseLong("473", 10)      // returns 473L
 Long.parseLong("-0", 10)       // returns 0L
 Long.parseLong("-FF", 16)      // returns -255L
 Long.parseLong("1100110", 2)   // returns 102L
 Long.parseLong("99", 8)        // throws a NumberFormatException
 Long.parseLong("Hazelnut", 10) // throws a NumberFormatException
 Long.parseLong("Hazelnut", 36) // returns 1356099454469L
 Long.parseLong("999")          // returns 999L
Answer from Mike Christensen on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ convert string to long or long in java
Convert String to long or Long in Java | Baeldung
January 10, 2024 - Now, given our String we can convert it to a Long object using tryParse(): ... All of the options explored so far throw a NumberFormatException in the event of a non-parseable String. Therefore, if we want to avoid the possibility of this exception being thrown, we can use the static factory method tryParse() which returns null instead:
๐ŸŒ
Java67
java67.com โ€บ 2015 โ€บ 07 โ€บ how-to-parse-string-to-long-in-java.html
How to convert String to long in Java? Example | Java67
By the way, since we have auto-boxing in Java, which also used Long.valueOf() method, It's always better to use parseLong() method because it's readable, reveals the real intention, handles invalid input, and specifically designed to parse String ...
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 12 โ€บ how-to-convert-string-to-long-in-java-4-examples.html
4 Ways to Convert String to long in Java? Example Tutorial
Though you need to remember few things while dealing with long and String first of all long is the primitive type which is wrapped by the 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 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.
๐ŸŒ
Alvin Alexander
alvinalexander.com โ€บ java โ€บ edu โ€บ qanda โ€บ pjqa00011.shtml
How do I convert a String to a long with Java? | alvinalexander.com
Question: How do I convert a String to a long with Java? Answer: The Java Long class includes a method named parseLong that serves just this purpose. An example of a simple program that performs this conversion is shown below: public class StringToLong { public static void main (String[] args) ...
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ String-to-long-in-Java
String to long in Java
Every numeric wrapper class has a parse method that can be used to convert a text String to the associated primitive type: Integer has parseInt(String text). Short has parseShort(String text). Byte has parseByte(String text). Each of these methods return a whole number that fits within the range of a long in Java.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-string-to-long
Java String to long - Javatpoint
Java Convert String to long example and examples of string to int, int to string, string to date, date to string, string to boolean, long to string, string to char, char to string, int to long, long to int etc.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ string to long in java
Convert String to Long in Java - Scaler Topics
June 30, 2022 - If the string contains anything except digits (or a minus sign at the beginning), the compiler raises a NumberFormatException. ... Here str is a string of digits. ... In the above example, we used the parseLong() function to convert a string ...
Find elsewhere
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ string โ€บ convert string to long in java
Convert String to long in Java
January 8, 2023 - String number = "2018"; //String long value1 = Long.parseLong( number, 10 ); long value2 = Long.valueOf( number ); long value3 = new Long( number ); The Long.valueOf() method parses the input string to a signed decimal long type.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-convert-string-to-long-in-java
Java String to long Conversion
In this tutorial, you will learn how to convert String to long in Java. We can use Long wrapper class for this purpose, this class contains couple of methods that we can use for this conversion. There are following three ways to convert a String to a long value. ... Long.parseLong(String): All the characters in the String must be digits except the first character, which can be a digit or a minus โ€˜-โ€˜. For example: long var = Long.parseInt("-123"); is allowed and the value of var after conversion would be -123.
๐ŸŒ
Dirask
dirask.com โ€บ posts โ€บ Java-convert-String-to-long-Kj8xAj
Java - convert String to long
public class Example2 { public static void main(String[] args) { String str = "123"; Long num = Long.valueOf(str); System.out.println(num); // 123 } } ... public class Example3 { public static void main(String[] args) { // Long.parseLong() - returns long System.out.println(Long.parseLong("123")); // 123 System.out.println(Long.parseLong("+123")); // 123 System.out.println(Long.parseLong("-123")); // -123 // Long.valueOf() - returns Long System.out.println(Long.valueOf("123")); // 123 System.out.println(Long.valueOf("+123")); // 123 System.out.println(Long.valueOf("-123")); // -123 } }
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-string-to-long
Convert String to Long in Java
January 9, 2023 - As Long.parseLong() can throw NullPointerException or NumberFormatException, it is a good practice to surround the function parseLong() with Java Try Catch and handle the exceptions accordingly. ... /** * Java Program - Convert String to Long */ public class StringToLong { public static void main(String[] args) { //a string String str = "85612536"; long n = 0; try { //convert string to long n = Long.parseLong(str); } catch (NumberFormatException e) { System.out.println("Check the string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-convert-string-to-long
Java Program to Convert String to Long - GeeksforGeeks
July 23, 2025 - // Java Program to convert String to Long using parseLong() Method public class GFG { public static void main(String args[]) { // Creating a custom string String s = "999999999999"; System.out.println("String: " + s); // Converting into Long ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 376931 โ€บ java โ€บ Converting-String-long
Converting String to long (Java in General forum at Coderanch)
How can I shave off the first x zero's from my string before converting it to a long. I am currently getting a numberformatexception. Thanks. ... My suggestion would be convert it to StringBuffer, then start checking whether the first character is '0', and delete it if it is until something else comes up. Forgive me for such a roundabout description, but I thought you should have the pleasure of coming up with the logic yourself! Look at the javadoc for StringBuffer for details on the methods available!
Top answer
1 of 5
17

Have a close look at the return types:

  1. Long.parseLong(String) returns a primitive long, so there will be re-boxing in this case: Long a = Long.parseLong(str);.
  2. new Long(String) will create a new Long object in every case. So, don't do this, but go for 3)
  3. Long.valueOf(String) returns a Long object, and will return cached instances for certain values -- so if you require a Long this is the preferred variant.

Inspecting the java.lang.Long source, the cache contains the following values (Sun JDK 1.8):

private static class LongCache {
    private LongCache(){}

    static final Long cache[] = new Long[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Long(i - 128);
    }
}
2 of 5
10

The best approach is Long.valueOf(str) as it relies on Long.valueOf(long) which uses an internal cache making it more efficient since it will reuse if needed the cached instances of Long going from -128 to 127 included.

Returns a Long instance representing the specified long value. If a new Long instance is not required, this method should generally be used in preference to the constructor Long(long), as this method is likely to yield significantly better space and time performance by caching frequently requested values. Note that unlike the corresponding method in the Integer class, this method is not required to cache values within a particular range.

Generally speaking, it is a good practice to use the static factory method valueOf(str) of a wrapper class like Integer, Boolean, Long, ... since most of them reuse instances whenever it is possible making them potentially more efficient in term of memory footprint than the corresponding parse methods or constructors.


Excerpt from Effective Java Item 1 written by Joshua Bloch:

You can often avoid creating unnecessary objects by using static factory methods (Item 1) in preference to constructors on immutable classes that provide both. For example, the static factory method Boolean.valueOf(String) is almost always preferable to the constructor Boolean(String). The constructor creates a new object each time itโ€™s called, while the static factory method is never required to do so and wonโ€™t in practice.

๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 58998-how-to-convert-a-string-to-a-long-in-java
[Java] - How to convert a String to a long in Java? - | SheCodes
Learn how to use the Long.parseLong() method in Java to convert a String to a long value and handle NumberFormatExceptions. ... How would you explain the purpose and usage of try, catch, throw, and finally blocks in a way that's easy to understand?
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ java-string-to-long
Java String to long - Tpoint Tech
Print Pattern in Java (Without Using a loop)- Starting With n, Replace n with n - 5, nโˆ’5...
๐ŸŒ
Dariawan
dariawan.com โ€บ tutorials โ€บ java โ€บ java-convert-string-long
Java - Convert String to long | Dariawan
static long toLong(String str, long defaultValue): Convert a String to a long, returning a default value if the conversion fails.