Convert Long to String in Java Convert Long to String in Java

Page content

In this tutorial, we’ll learn different methods to convert long primitive or Long object to String in Java

Convert long primitive to String

String s1 = Long.toString(1L); // "1"
String s2 = String.valueOf(1L);  // "1"
String s3 = "" + 1L; // "1"

String s4 = new StringBuilder().append(1L).toString(); // "1"
String s5 = String.format("%d", 1L); // "1"
String s6 = new DecimalFormat("#").format(1L); // "1"

We see that first three methods i.e. Long.toString(), String.valueOf, and + are concise and good to use.

Convert Long object to String

Long longObj = 1L;

String s1 = longObj.toString();  // "1"

String s2 = Long.toString(longObj); // "1"
String s3 = String.valueOf(longObj);  // "1"
String s4 = "" + longObj; // "1"

String s5 = new StringBuilder().append(longObj).toString(); // "1"
String s6 = String.format("%d", longObj); // "1"
String s7 = new DecimalFormat("#").format(longObj); // "1"

We see that first method i.e. longObj.toString() is the quickest and concise way to convert a Long object to String, followed by the next three methods.

Exception handling - Long to String

Let’s see how these methods handle the null value:-

Long longObj = null;

String s1 = longObj.toString();  // throw "NullPointerException"
String s2 = Long.toString(longObj); // throw "NullPointerException"

String s3 = String.valueOf(longObj);  // "null"
String s4 = "" + longObj; // "null"
String s5 = new StringBuilder().append(longObj).toString(); // "null"
String s6 = String.format("%d", longObj); // "null"

String s7 = new DecimalFormat("#").format(longObj); // throw "IllegalArgumentException"

Only s1 and s2 throw. longObj.toString() fails because it’s a plain instance call on a null reference, and Long.toString(longObj) fails because passing a null Long where a primitive long is expected forces an auto-unboxing that throws NullPointerException before the method body even runs.

s3 is the one worth double-checking, because it’s easy to assume String.valueOf(Long) behaves like Long.toString(Long) and also unboxes. It doesn’t: the compiler resolves String.valueOf(longObj) to the String.valueOf(Object) overload (a Long is-a Object, so no unboxing is needed to make the call type-check), and String.valueOf(Object) explicitly returns the string "null" for a null argument instead of throwing — verified above, s3 is "null", not an exception. That puts it in the same “safe” group as s4, s5, and s6 (highlighted), which all go through code paths that print "null" for a null reference rather than unboxing it.

s7, new DecimalFormat("#").format(longObj), throws IllegalArgumentException instead — Format.format(Object) needs an actual Number to format and rejects null up front, rather than either unboxing it or falling back to a "null" string.

So: pick s1/s2 only when a null should be a loud failure; pick s3/s4/s5/s6 when you’d rather see the literal string "null" than crash; avoid relying on DecimalFormat for a value that might be null unless you’re prepared to catch IllegalArgumentException.

Converting with a Different Radix

Long.toString(long, int) and its named shortcuts convert to bases other than 10, which is handy for hex or binary output — useful for things like flags, IDs, or debugging output:

System.out.println(Long.toString(255, 16)); // "ff"
System.out.println(Long.toHexString(255));  // "ff"
System.out.println(Long.toBinaryString(10)); // "1010"

Long.toHexString and Long.toBinaryString treat the long as unsigned for this purpose, so they’re also the right tool if you ever need to print the raw bits of a negative long without a sign getting in the way.

Which One Is Fastest?

String.valueOf(long) is implemented as a direct call to Long.toString(long) — they’re the same operation with different names, so there’s no reason to prefer one over the other on performance grounds; pick whichever reads better at the call site. new StringBuilder().append(...).toString() and String.format("%d", ...) both do strictly more work for a single value: the StringBuilder path allocates a builder object in addition to the final String, and String.format has to parse the "%d" format string and go through Formatter machinery on every call. For converting one long (or Long) to a String, prefer Long.toString() / String.valueOf() (or plain + concatenation, which the compiler already optimizes for you); save StringBuilder for building up multiple pieces and String.format/DecimalFormat for when you actually need their formatting features, like a specific radix, padding, or grouping separators.