How to convert a long to a Java String

Converting a long to a String is straightforward in modern Java. For an explicit conversion, Long.toString(longValue) or String.valueOf(longValue) clearly communicates intent. String concatenation also works, but it is usually better suited to building a larger message than to conversion by itself.

// Java 25 long-to-String example
long value = 90210L;

String text = Long.toString(value);

IO.println("Converted value: " + text);
IO.println("String length: " + text.length());

Convert from long to String in Java

If the empty String addition trick is not to your liking, rest assured there are many ways to convert a long to a String in Java. Options include the following:

  1. Add it to an empty String, “” + longToConvert.
  2. The String class’ valueOf method, String.valueOf( longToConvert ).
  3. The Long class’ toString method,  Long.toString( longToConvert ).
  4. The String class’ format method,  String.format( “%d”, longToConvert ).
  5. Use DecimalFormat, new DecimalFormat(“#”).format(longToConvert).

Which long-to-String conversion should you use?

For a standalone conversion, prefer Long.toString(value) or String.valueOf(value). Both are concise, familiar and explicit.

If the number is part of a larger message, concatenation or String.formatted can be clearer. Use DecimalFormat when you actually need numeric formatting rules rather than a basic conversion.

Example long to String conversion code

Here’s a JShell script you can run that demonstrates the five approaches to the long to String conversion problem listed above:

// Modern ways to convert a long to String in Java 25
long value = 90210L;

String a = Long.toString(value);
String b = String.valueOf(value);
String c = "%d".formatted(value);
String d = "" + value;
String e = new java.text.DecimalFormat("#").format(value);

IO.println(a);
IO.println(b);
IO.println(c);
IO.println(d);
IO.println(e);

Add a long inside a text String

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.

Compare the JShell program below, which first uses the plus operator to add a long inside a String, and then the printf option second:

long value = 90210L;

IO.println("The long is " + value + " inside the text.");

String message = "The long is %d inside the text.".formatted(value);
IO.println(message);

The use of printf makes embedding primitive types like a long in a String much easier.

float double Java types

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.


Cameron McKenzie Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.