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.
๐ŸŒ
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.
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ long-to-String-in-Java
long to String in Java
The conversion of a long to a String with the plus operator works with any of the eight Java primitive types. By the way, you can also go the other way. Here are several ways to convert a String to a long in Java.
๐ŸŒ
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?