Java long to String – Tutorial
Today we will see different ways to convert long to string in java. Java long to string conversion can be done in many ways, we will go through them one by one with example code snippets.
Let’s look at different code snippets for java long to string conversion. Note that long is a primitive data type whereas Long is an Object. However java supports autoboxing, so they both can be used interchangeably in most of the cases.
Using + operator
This is the easiest way to convert long to string in java.
long l = 123L;
String str = l+""; // str is '123'
Long.toString()
We can use Long class toString method to get the string representation of long in decimal points. Below code snippet shows you how to use it to convert long to string in java.
long l = 12345L;
String str = Long.toString(l);
System.out.println(str); //prints '12345'
We can write long in other formats too such as Octal and Hexadecimal, let’s see what happens when we convert long to string in those scenarios.
long l = 0x11L;
String str = Long.toString(l);
System.out.println(str); //prints '17'
l = 011L;
str = Long.toString(l);
System.out.println(str); //prints '9'
So the string is always being returned in decimal format. Let’s look into other ways to convert long to string too.
String.valueOf()
long l = 12345L;
String str = String.valueOf(l); // str is '12345'
new Long(long l)
Long constructor with long argument has been deprecated in Java 9, but you should know it.
long l = 12345L;
//deprecated from Java 9, use valueOf for better performance
String str = new Long(l).toString(); // str is '12345'
String.format()
long l = 369L;
String s = String.format("%d", l);
DecimalFormat
long l = 12345L;
String str = DecimalFormat.getNumberInstance().format(l);
System.out.println(str); //str is '12,345'
//if you don't want formatting
str = new DecimalFormat("#").format(l);
System.out.println(str); //str is '12345'
StringBuilder, StringBuffer
We can use StringBuilder and StringBuffer append function to convert long to string.
long l = 12345L;
String str = new StringBuilder().append(l).toString();
Java Long to String Example
Here is a simple program where we will convert long to string and print it using all the different methods we saw above.
package com.journaldev.string;
import java.text.DecimalFormat;
public class JavaLongToString {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
long l = 12345L;
String str = Long.toString(l);
System.out.println(str);
str = String.valueOf(l);
System.out.println(str);
// deprecated from Java 9, use valueOf for better performance
str = new Long(l).toString();
System.out.println(str);
str = String.format("%d", l);
System.out.println(str);
str = l + "";
System.out.println(str);
str = DecimalFormat.getNumberInstance().format(l);
System.out.println(str);
str = new DecimalFormat("#").format(l);
System.out.println(str);
str = new StringBuilder().append(l).toString();
System.out.println(str);
}
}
Change the long value in above program to Octal or Hexadecimal format, you will notice that string representation is always on decimal format.
Convert Long to String with Radix
What if we want to convert long to string with different radix, not the default decimal point. We can use Long class methods for these cases. Below code snippet shows how to get the string representation of a long variable in different bases such as Octal, Hex or even custom one.
long number = 45;
System.out.println(Long.toBinaryString(number)); //101101
System.out.println(Long.toOctalString(number)); //55
System.out.println(Long.toHexString(number)); //2d
System.out.println(Long.toString(number, 5)); //140
That’s all for converting long to string in java program.