Java – Convert Long to String
You can convert an long to string in Java using String class methods.
In this tutorial, we shall go through some of the ways to convert a long to string, with example Java programs.
Long to String using Long.toString() method
toString() is a static method in Long class that returns string object representing the specified long value. We have to pass the long value as argument to toString() method.
In the following example we shall convert a number of long datatype to string by using Long.toString() function.
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
Long to String using String Concatenation
This is the most easiest way to convert an longing point number to a string. All you have to do is add an empty string to it.
The concatenation operator takes long and empty string as operands and returns a string.
In the following example we shall convert a long to string by concatenating the number to an empty string.
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
Long to String using String.valueOf() method
We can use String.valueOf() method to get a string value from a number of datatype long. Pass the long as argument to String.valueOf() function, and the function returns a String object.
In the following example we shall convert a number of long datatype to string by using String.valueOf() function.
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
Long to String using StringBuffer.append() method
Create a StringBuilder object and append the long value to it using append() method. append() method appends the string representation of the long to this sequence. After append() function, call toString() method on the StringBuilder object, it returns string.
You can create StringBuilder object and call append() function and toString() function as chain in a single statement.
In the following example we shall convert a number of long datatype to string by using StringBuilder class.
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
Conclusion
In this Java Tutorial, we learned how to convert or typecast a long to string in Java, using String concatenation or String.valueOf() method.