You can use the NumberUtils from Apache Commons. It's null-safe and you can optionally specify a default value.

Example:

NumberUtils.toLong(null) = 0L
NumberUtils.toLong("")   = 0L
NumberUtils.toLong("1")  = 1L

NumberUtils.toLong(null, 1L) = 1L
NumberUtils.toLong("", 1L)   = 1L
NumberUtils.toLong("1", 0L)  = 1L

For more info, check the API.

Answer from Ehler on Stack Overflow
🌐
LabEx
labex.io › tutorials › java-how-to-ensure-the-safety-and-robustness-of-long-to-string-conversion-in-java-414020
How to ensure the safety and robustness of long to string conversion in Java | LabEx
Test various scenarios, including null values, extreme values, and error conditions, to validate the correctness and reliability of your implementation. By following these best practices and recommendations, you can ensure the safety and robustness of long-to-string conversion in your Java applications, providing a reliable and user-friendly experience for your users.
🌐
BeginnersBook
beginnersbook.com › 2015 › 05 › java-long-to-string
Java long to String Conversion
Last Updated: November 20, 2022 ... · Using StringBuilder and StringBuffer · Note: Out of all these ways, the String.valueOf() is the preferred method for conversion as it is null safe....
🌐
Baeldung
baeldung.com › home › java › java string › convert long to string in java
Convert Long to String in Java | Baeldung
October 4, 2021 - Be careful because if obj is null, we’ll get an IllegalArgumentException. In summary, we’ve learned different ways to convert Long to String in Java.
🌐
Java67
java67.com › 2015 › 07 › how-to-parse-string-to-long-in-java.html
How to convert String to long in Java? Example | Java67
public Long(String s) throws NumberFormatException { this.value = parseLong(s, 10); } 3) The real method, parseLong() which does all the work of parsing throws NumberFormatException for invalid inputs e.g. null values, empty values, lonely plus or minus sign, alphanumeric String, and String with out of range long values. Here is a nice summary of different ways to convert String to Long object and long primitive in Java:
🌐
TutorialKart
tutorialkart.com › java › java-string-to-long
Convert String to Long in Java
January 9, 2023 - Exception in thread "main" java.lang.NumberFormatException: For input string: "5.354" at java.base/java.lang.NumberFormatException.forInputString(Unknown Source) at java.base/java.lang.Long.parseLong(Unknown Source) at java.base/java.lang.Long.parseLong(Unknown Source) at StringToLong.main(StringToLong.java:12) Also, if null is passed to Long.parseLong(), the function throws NullPointerException. ... /** * Java Program - Convert String to Long */ public class StringToLong { public static void main(String[] args) { //a string String str = null; //convert string to long long n = Long.parseLong(str); System.out.print(n); } }
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-3.4 › index.html
Apache Commons Lang 3.4 API
JavaScript is disabled on your browser · Frame Alert · This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to Non-frame version
Find elsewhere
🌐
Verve AI
vervecopilot.com › interview-questions › why-is-mastering-java-long-to-string-essential-for-robust-java-development
Why Is Mastering Java Long To String Essential For Robust Java Development
They explicitly state the intention to convert the long to a String, making the code easier to read and understand for other developers. String.valueOf(Long obj) is also preferred for Long objects due to its null-safety.
🌐
GitHub
github.com › rapidsai › cudf › issues › 5110
[BUG] Cast from string to long should return null if string is not a valid long · Issue #5110 · rapidsai/cudf
January 3, 2020 - @Test void testCastStringToLongOverflow() { String outOfRangeLong = "9223372036854775808"; // Long.MAX_VALUE + 1 try (ColumnVector strings = ColumnVector.fromStrings(outOfRangeLong); ColumnVector cv = strings.castTo(DType.INT64); HostColumnVector longs = cv.copyToHost();) { assert(longs.isNull(0)); } }
Published   May 06, 2020
🌐
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) { System.out.println("NumberFormatException: " + nfe.getMessage()); } } } Note that I call trim() on the string I’m converting.
🌐
Tabnine
tabnine.com › home page › code › java › java.lang.long
java.lang.Long.parseLong java code examples | Tabnine
@Override public long contentLength() { try { return contentLength != null ? Long.parseLong(contentLength) : -1; } catch (NumberFormatException e) { return -1; } } ... private static long stringToLong(String s) { if (s == null) return -1; try { return Long.parseLong(s); } catch (NumberFormatException e) { return -1; } }
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › String-to-long-in-Java
String to long in Java
The correct way to convert a String to long in Java is with the parseLong method of the wrapper class. The constructor conversion approach is deprecated! Use parseLong instead.
🌐
Medium
medium.com › hackernoon › safely-tostring-a-java-object-a3d6ea3c404
Safely toString a Java object. Avoiding NPE | by Orléando Dassi | HackerNoon.com | Medium
December 31, 2023 - The printed string of our ObjectA into the console is: ObjectA{propA=’Fake propA’, objectB1=ObjectB{propB=’Fake propB’}, objectB2=null} More usages of the Objects class can be found on this page. To sum up, we have seen here a proper way to safely toString an object with embed properties.
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 76514512 › null-safe-integer-longvalue-in-java
Null-safe Integer.longValue() in Java? - Stack Overflow
Especially when value is not just a value but is actually some function with long name and multiple arguments. Maybe I have missed something and there is such a method in any of the popular libraries? ... Java generally doesn't do "short and convenient". I recommend upgrading to Kotlin, which has null-safety built in: intValue?.toLong().