Java examples of converting a long value to a String in two different ways. The first example uses String.valueOf(), and the second uses Long.toString() methods. Both are static methods and call upon String and Long classes directly.
1. Quick Reference
long number = 123456789L;
String strValue1 = String.valueOf(number);
String strValue2 = Long.toString(number);
2. Using String.valueOf()
String.valueOf() method returns the String representation of the long argument. 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
3. Using Long.toString()
Long.toString(long) method returns the string representation of the long argument. Note that it returns “null” string for a NULL value.
long number = 123456789L;
String strValue = Long.toString(number); //long to String conversion
//Verify the result
System.out.println(strValue); //123456789
Read this post to learn how to convert String to Long in Java.
Happy Learning !!