Java – Convert Long to String
To convert a long value to a String in Java, use Long.toString(long) or String.valueOf(long). Both return the decimal string representation of the value.
String concatenation and a mutable string buffer can also produce a string from a long, but the direct conversion methods are clearer when conversion is the only goal.
Java long to String conversion syntax
String str1 = Long.toString(longValue);
String str2 = String.valueOf(longValue);
For example, if longValue is 12345L, both statements produce the string "12345". Negative values and zero are converted in the same way.
Convert long to String using Long.toString()
Long.toString(long) is a static method of the Long class. Pass the primitive long value as its argument, and the method returns a String containing that value in decimal form.
In the following example, positive and negative long values are converted using Long.toString().
Java Program
/**
* Java Program - Convert Long to String
*/
public class LongToString {
public static void main(String[] args) {
String str = Long.toString(16569333869L);
System.out.println(str);
str = Long.toString(-936622185233L);
System.out.println(str);
}
}
Output
16569333869
-936622185233
This method is a good choice when you want the conversion to explicitly show that the source value is a long.
Convert long to String using String.valueOf()
String.valueOf(long) accepts a primitive long and returns its string representation. For a primitive long, it produces the same decimal text as Long.toString(long).
In the following example, the long value is passed to String.valueOf().
Java Program
/**
* Java Program - Convert Long to String
*/
public class LongToString {
public static void main(String[] args) {
long n = 524839L;
//convert long to string using String methods
String str = String.valueOf(n);
System.out.print(str);
}
}
Long value has been converted to String.
Output
524839
String.valueOf(long) is especially convenient when nearby code already uses String.valueOf() for values of different primitive types.
Convert long to String using string concatenation
A long can also be converted by concatenating it with an empty string. When one operand of + is a String, Java performs string concatenation and converts the numeric operand to text.
In the following example, the long value is concatenated with "".
Java Program
/**
* Java Program - Convert Long to String
*/
public class LongToString {
public static void main(String[] args) {
long n = 5252639L;
//convert long to string using string concatenation
String str = n + "";
System.out.print(str);
}
}
Output
5252639
This form works, but Long.toString() or String.valueOf() communicates the conversion more directly when no other text is being concatenated.
Convert long to String using StringBuffer.append()
StringBuffer.append(long) appends the string representation of a long value to the buffer. Calling toString() on the buffer then returns a String.
This approach is more relevant when you are already building a larger piece of text in a StringBuffer. For a single numeric conversion, a direct method such as Long.toString() or String.valueOf() is simpler.
In the following example, append() and toString() are chained in one statement.
Java Program
/**
* Java Program - Convert Long to String
*/
public class LongToString {
public static void main(String[] args) {
String str = (new StringBuffer()).append(163869L).toString();
System.out.println(str);
str = (new StringBuffer()).append(-25963869L).toString();
System.out.println(str);
}
}
Output
163869
-25963869
Long.toString() vs String.valueOf() for a long value
| Method | Use for primitive long? | When it is useful |
|---|---|---|
Long.toString(value) | Yes | Explicitly converting a long value to text |
String.valueOf(value) | Yes | General-purpose conversion to String |
value + "" | Yes | Works through string concatenation, but is less explicit for conversion-only code |
new StringBuffer().append(value).toString() | Yes | Useful when a StringBuffer is already being used to build text |
For ordinary long-to-String conversion, prefer Long.toString(value) or String.valueOf(value). They state the intent clearly and do not require building an intermediate text expression.
Primitive long and nullable Long objects
A primitive long cannot be null, so either direct conversion method can be used safely with any valid long value. A Long wrapper object can be null, which changes the behavior of some expressions.
Long number = null;
String text = String.valueOf(number);
System.out.println(text);
Here, String.valueOf(number) returns the text "null". By contrast, calling number.toString() when number is null throws a NullPointerException.
Choosing a Java long-to-String conversion method
Use Long.toString(long) when you want a method specific to the long type, or String.valueOf(long) when you prefer Java’s general string-conversion method. String concatenation also works, while StringBuffer.append() is better suited to code that is already assembling larger text.
In this Java Tutorial, we learned how to convert a long value to a String in Java using Long.toString(), String.valueOf(), string concatenation, and StringBuffer.append().
TutorialKart.com