Java example to convert long to String in two different ways. First example is using String.valueOf(long l) and second example is to use Long.toString(long l) methods. Both are static methods and called upon String and Long classes directly.
1. long to String Conversion – Quick Reference
long number = 123456789L; //Method - 1 String strValue1 = String.valueOf(number); //Method - 2 String strValue2 = Long.toString(number); //Verify the result System.out.println(strValue1); //123456789 System.out.println(strValue2); //123456789
2. String.valueOf(long l) method to convert long to String
String.valueOf(long l) method returns the string representation of the long argument. The representation is exactly the one returned by the Long.toString(long l)
method given below.
Example of convert long to string using String.valueOf() method
public class StringExample { public static void main(String[] args) { long number = 123456789L; String strValue = String.valueOf(number); //long to String conversion //Verify the result System.out.println(strValue); //123456789 } }
3. Long.toString(long l) method to convert long to String
Long.toString(long l) method returns the string representation of the long argument. The representation is exactly the one returned by the String.valueOf(long l)
method shown above.
Example of convert long to string using Long.toString() method
public class StringExample { public static void main(String[] args) { 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 !!
is it intentional that Long.toString returns representation and Integer.toString returns object of string ?