In this guide, we will discuss the following ways to convert a long value to a String:
- String.valueOf(long l) Method
- Long.toString(long l) Method
- String.format() Method
- Using DecimalFormat
- Using StringBuilder and StringBuffer
Note: Out of all these ways, the String.valueOf()
is the preferred method for conversion as it is null safe. If the long value is null
, the String contains "null"
after conversion, instead of throwing NullPointerException
.
1. Using String.valueOf() Method
We can use String.valueOf(long l) method to convert long to String. This method takes a long value as an argument and returns a string representation of it.
Method Syntax:
public static String valueOf(long l)
Parameters:l
– long value that needs to be converted into a String
Returns:
String representation of long argument l
.
long lvar = 123; String str = String.valueOf(lvar);
Java long to String Example using String.valueOf()
public class JavaExample{ public static void main(String args[]){ long l = 100000001L; //long to String Conversion String str = String.valueOf(l); //Print String value System.out.println("String after conversion: "+str); } }
Output:
2. Using Long.toString() Method
This is the another way of long to String conversion. The method Long.toString(long l) works same as String.valueOf(long)
method. It returns the string representation of the long value passed as an argument to this method. For example, if the passed long value is 1202 then the returned string would be “1202”.
Method Syntax:
public static String toString(long l)
Parameters:l
– long value whose String representation is to be determined.
Returns:
String representation of the given long value l
.
long lvar2 = 200; String str2 = Long.toString(lvar2);
Note: If the long value is null then it throws NullPointerException
, it is better to use String.valueOf()
method in this case.
Java long to String Example using Long.toString()
public class JavaExample{ public static void main(String args[]){ //long values are suffixed with 'L' long l = 123456789L; //passing the long value to the toString() method String str = Long.toString(l); //Print the string value returned by Long.toString() method System.out.println("String after conversion: "+str); } }
Output:
3. Using String.format() Method
String.format() was introduced in java 5. We can use this method to convert a long value to String as shown in the following program.
public class JavaExample{ public static void main(String args[]){ long l = 1234567L; String str = String.format("%d",l); //Output: "1234567" System.out.println("long to String: "+str); } }
This method is especially useful, if you want to add leading zeroes in the output. Let’s see how can we add leading zeroes in the output String.
public class JavaExample { public static void main(String[] args) { long l = 12345L; // In %09: // 0: pad string with zeroes // 9: set the length of output string to 9 String str = String.format("%09d", l); System.out.println("long to String with leading zeroes: "+str); } }
Output:
4. Using DecimalFormat
You can use DecimalFormat to convert the given long value to String with or without commas.
With Commas:
import java.text.*; public class JavaExample{ public static void main(String args[]) { long l = 12345678L; String str = DecimalFormat.getNumberInstance().format(l); System.out.println("long to String: "+str); } }
Output:
Without Commas:
import java.text.*; public class JavaExample{ public static void main(String args[]) { long l = 12345678L; //Specify the pattern '#' to avoid commas in the output String str = new DecimalFormat("#").format(l);; System.out.println("long to String: "+str); } }
Output:
5. Using StringBuffer and StringBuilder
We can also use StringBuffer and StringBuilder class to convert the given long primitive value or Long object to a String.
public class JavaExample{ public static void main(String args[]) { long l = 1000001L; Long obj = 1000005L; String str = new StringBuilder().append(l).toString(); String str2 = new StringBuilder().append(obj).toString(); String str3 = new StringBuffer().append(l).toString(); String str4 = new StringBuffer().append(obj).toString(); System.out.println(str); //Output: "1000001" System.out.println(str2); //Output: "1000005" System.out.println(str3); //Output: "1000001" System.out.println(str4); //Output: "1000005" } }
Leave a Reply