There are many scenarios where we may be required to convert a long to a String. This post will discuss how we can convert a long integer to a string type in Java.
- Long.toString() method
- String.valueOf() method
- Using + operator
- Using StringBuffer or StringBuilder append() function
- String.format method
Let’s look at all the above methods one by one.
Long.toString() method
Java has a Long wrapper class, so we can perform class operations on the long primitive type. The Long class provides a static toString() method that can covert a long primitive value into its corresponding String object.
Declaration of toString() method
public static String toString(long i)
To convert long into String, we have to write the following line.
String str = Long.toString(longValue);
The whole program of converting long to String is shown below.
public class Codekru {
public static void main(String[] args) {
long l = 3051234567L;
String str = Long.toString(l);
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "3051234567"
}
}
Output –
true
3051234567
So, here we have converted a long primitive into a String which is also proved by the instanceOf method.
String.ValueOf() method
String class also has a static valueOf() method, which will take a long primitive as an argument and return a String object. Below is the declaration of valueOf() method.
public static String valueOf(long l)
Do you know that String .valueOf() method internally uses Long.toString() method.
The internal implementation of String.valueOf() function
public static String valueOf(long l) {
return Long.toString(l);
}
And Below is an example where we have converted a long primitive value to a String using the valueOf() method.
public class Codekru {
public static void main(String[] args) {
long l = 3051234567L;
String str = String.valueOf(l);
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "3051234567"
}
}
Output –
true
3051234567
Using + operator
We can directly convert the long to a String using the + operator. Yes, it’s that easy 😛
public class Codekru {
public static void main(String[] args) {
long l = 3051234567L;
String str = "";
str = str + l + ""; // acting as a string concatenation operator here
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "3051234567"
}
}
true
3051234567
Using StringBuffer or StringBuilder append() function
We can also use the append() function in StringBuilder and StringBuffer classes to convert long to a String.
public class Codekru {
public static void main(String[] args) {
long l = 3051234567L;
String str = new StringBuffer().append(l).toString();
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "305"
}
}
Output –
true
3051234567
String.format() method
Besides using the valueOf() method, we can also use the static format() method to convert long into its corresponding string.
public class Codekru {
public static void main(String[] args) {
long l = 3051234567L;
String str = String.format("%d", l);
System.out.println(str);
}
}
Output –
3051234567
Hope you liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.