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
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-string-to-long
Java Program to Convert String to Long - GeeksforGeeks
July 23, 2025 - Long.parseLong() method to convert a string to a primitive long. ... // 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 = ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › String-to-long-in-Java
String to long in Java
The benefits of the parseLong(String) method to convert a String to a long or int in Java instead of the valueOf(String) or the deprecated contructor method include: The Long.parseLong() method returns a long primitive, not a wrapper.
🌐
Java67
java67.com › 2015 › 07 › how-to-parse-string-to-long-in-java.html
How to convert String to long in Java? Example | Java67
You can also use the constructor of the Long class which accepts a String and returns a Long object, but internally it also uses the parseLong() method. BTW, if you have just started learning Java or looking forward to learning Java from scratch, I suggest you take a look at Cay S. Horstmann's Core Java Volume 1, 9th Edition book. You will learn most of the Java fundamentals in a quick time. There are three main ways to convert a numeric String to Long in Java, though internally all of them use the parseLong() method to parse numeric String to Long value.
🌐
Java67
java67.com › 2015 › 07 › how-to-convert-long-value-to-string-in-java-example.html
How to convert long to String in Java? Example | Java67
Here is our sample Java program for this data type conversion. In this program, you will learn all these three methods of converting primitive long to String in Java.
🌐
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
In this Java tutorial, we will learn 4 different ways to convert String to long by code examples and we will also analyze the pros and cons of each approach.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-convert-string-to-long-in-java
Java String to long Conversion
public class JavaExample { public ...intln("nInvalid String to long Conversion:"); convertStringToLong(invalidStr); } public static void convertStringToLong(String str) { try { long number = Long.parseLong(str); System.out.println("The long value is: " + number); } catch ...
🌐
How to do in Java
howtodoinjava.com › home › string › convert string to long in java
Convert String to long in Java
January 8, 2023 - Java examples of converting a String to a long type using Long.parseLong(String), Long.valueOf(String) and new Long(String) constructor.
Find elsewhere
🌐
Makeinjava
makeinjava.com › home › convert string variable to long in java (example)
Convert String variable to Long in java (example)
January 4, 2024 - 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); Java’s Scanner class provides the facility to structured ...
🌐
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 - Given our String, we can use the overloaded Long constructor that takes a String as an argument: ... This creates a new Long instance which can be converted to a primitive long by invoking the longValue() method.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › long-to-String-in-Java
long to String in Java
The easiest way to convert a long to a String is to append it to double quote. However, for complex output where a long must be embedded in a Java String, use the printf or String.format method.
🌐
Scaler
scaler.com › home › topics › string to long in java
Convert String to Long in Java - Scaler Topics
June 30, 2022 - In the above example, we used the parseLong() function to convert a string composed of digits into a Long.
🌐
How to do in Java
howtodoinjava.com › home › string › convert long to string in java
Convert long to String in Java
January 10, 2023 - This value is exactly the one returned by the Long.toString(long) method given below. long number = 123456789L; String strValue = String.valueOf(number); //long to String conversion //Verify the result System.out.println(strValue); //123456789
🌐
TutorialsPoint
tutorialspoint.com › convert-from-string-to-long-in-java
Convert from String to long in Java
public class Demo { public static void main(String[] args) { String myStr = "5"; Long myLong = Long.parseLong(myStr); System.out.println("Long: "+myLong); } } ... The following is an example wherein we have used valueOf() and longValue() method to convert String to long.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Long.html
Long (Java SE 17 & JDK 17)
January 20, 2026 - ... Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor. Use parseLong(String) to convert a string to a long primitive, or use valueOf(String) to convert a string to a Long object.
🌐
Alvin Alexander
alvinalexander.com › java › edu › qanda › pjqa00011.shtml
How do I convert a String to a long with Java? | alvinalexander.com
public class StringToLong { public static void main (String[] args) { // String s = "fred"; // do this if you want an exception String s = "100"; try { long l = Long.parseLong(s.trim()); //<-- String to long here System.out.println("long l = " + l); } catch (NumberFormatException nfe) { ...
🌐
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?
🌐
Oracle
docs.oracle.com › en › java › javase › 20 › docs › api › java.base › java › lang › Long.html
Long (Java SE 20 & JDK 20)
July 10, 2023 - ... Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor. Use parseLong(String) to convert a string to a long primitive, or use valueOf(String) to convert a string to a Long object.