How to Convert long to String in Java

There are many ways we can convert long to String in java. Among them String.valueOf() and Long.toString() methods are quite common. In this tutorial I will be sharing 8 different ways to convert long to String with code snippets in java.

Read Also :  3 ways to convert double to int in java

I have shared below 8 different ways to convert long to String in Java but in tech industry you should use mostly two:

1. Convert long to String using String.valueOf(long) method  [Preferred]
2. Convert long to String using Long.toString(long) method

1. Using String.valueOf(long) method

It is one of the simplest method to convert long to String. Just pass the long value in the String valueOf(long) method , and it will convert long to String. Performance wise this method stands out in comparison to other long to String conversion methods.

public class LongToString {
    public static void main(String args[]) {
        long longvalue = 123L;
        String result = String.valueOf(longvalue);
        System.out.println("long to String "+ result);
    }
}
Output :
long to String 123

The other advantage of using valueOf() method is that it will automatically convert long values in other formats such as Octal and Hexadecimal to decimal format.

Example of long value in Hexadecimal format to decimal format using valueOf(long) method.

        long longvalue = 0x9c23L;
        String result = String.valueOf(longvalue);
        System.out.println("long to String "+ result); // Output : "39971"

Example of long value in Octal format to decimal format using valueOf(long) method.

        long longvalue = 0673L;
        String result = String.valueOf(longvalue);
        System.out.println("long to String "+ result); // Output : "443"

2. Using Long.toString(long) method

Long.toString(long) method is sufficient to convert long to String in java. Check here why you should prefer String.valueOf(long) over Long.toString(long) method.
 
public class LongToString {
    public static void main(String args[]) {
        long longvalue = 123L;
        String result = Long.toString(longvalue);
        System.out.println("long to String "+ result);
    }
}
Output :
long to String 123

Just like above String.valueOf(long) method, Long.toString(long) method also converts long value in Hexadecimal and Octal formats to decimal format automatically.

Example of long value in Hexadecimal format to decimal format using Long.toString(long) method.

        long longvalue = 0x9c23L;
        String result = Long.toString(longvalue);
        System.out.println("long to String "+ result);  // Output : "39971"  

Example of long value in Octal format to decimal format using Long.toString(long) method.

        long longvalue = 0673L;
        String result = Long.toString(longvalue);
        System.out.println("long to String "+ result); // Output : "443"   

3. Using String.format()

String.format() method has been introduced in java 5. This method is another alternative to convert Long to String in java.

        long longvalue = 123L;
        String result = String.format("%d",longvalue);
        System.out.println("long to String "+ result); // Output : "123"    

4. Using DecimalFormat

Here java.text.DecimalFormat class uses a certain pattern to convert number to String representation.
Below code will represent the output in format (i.e 123456 will represent as 123,456 (notice comma))

import java.text.*;
public class LongToString {
    public static void main(String args[]) {
        long longvalue = 123456L;
        String result = DecimalFormat.getNumberInstance().format(longvalue);
        System.out.println("long to String "+ result);    
    }
}
Output :
long to String 123,456

If you want the output without formatting (123456 without comma) then use below code.

        long longvalue = 123456L;
        String result = new DecimalFormat("#").format(longvalue);
        System.out.println("long to String "+ result);  
Output :
long to String 123456

5. Using StringBuilder or StringBuffer Class

We can use StringBuilder or StringBuffer class append method to convert long to String in java.

        long longvalue = 123456L;
        String result = new StringBuilder().append(longvalue).toString();
        System.out.println("long to String "+ result);  // Output : "123456"   

6. Using + operator

This may be the easiest method but I recommend not to use it in practice. In tech industry we are writing human readable code, and this method does not meet the criteria.
If you like this way then feel free to use it.

        long longvalue = 123456L;
        String result = longvalue + "";
        System.out.println("long to String "+ result);  // Output : "123456"   

7. Using Special Radix

If you want to convert long to String with different base(radix) other than default decimal point whose base(radix) is 10, then we can use Long class methods. Long class provides different methods to convert to Octal,Hexadecimal or Binary formats.

Examples :

Binary:

toBinaryString(long) method will convert long value in decimal format to Binary format String.

        long longvalue = 123456L;
        String result = Long.toBinaryString(longvalue);
        System.out.println("long to Binary String "+ result);    
Output :
long to Binary String 11110001001000000

The above result "11110001001000000" is the Binary representation of long 123456.

Octal:

toOctalString(long) method will convert long value in decimal format to Octal format String.

        long longvalue = 123456L;
        String result = Long.toOctalString(longvalue);
        System.out.println("long to Octal String "+ result);    
Output :
long to Octal String 361100

The above result "361100" is the Octal representation of long 123456.

Hexadecimal:

toHexString(long) method will convert long value in decimal format to Hexadecimal format String.

        long longvalue = 123456L;
        String result = Long.toHexString(longvalue);
        System.out.println("long to Hexadecimal String "+ result);    
Output :
long to Hexadecimal String 1e240

The above result "1e240" is the Hexadecimal representation of long 123456.

Custom Base(Radix):

We can use any other base(radix) system when converting Long to String in java. Below example shows base 9 number system.

        long longvalue = 123456L;
        String result = Long.toString(longvalue,9);
        System.out.println("long to Custom Base String "+ result);    
Output :
long to Custom Base String 207313

The above result "207313" is the representation of long number 123456 when written in 9 base number system.

8. Using new Long(long)

I kept this method last as this is deprecated in java 9. But you can still go through it for your knowledge.

        long longvalue = 123456L;
        String result = new Long(longvalue).toString();
        System.out.println("long to String "+ result); // Output : "123456"   


I have shared above 8 ways to convert long to String in java. Please write in comments if you have any questions.

References :
Long Oracle Doc

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry