Java long to String

In this post, we will see how to convert long to String in java.

There are lot of ways to convert long to String.Let’s see each one by one.
[toc]


Using Long.toString()

You can use Long class toString() method to convert long to String.

long i = 22L;
String longStr = Long.toString(i);
System.out.println(longStr); //output '22'
In case, Long can be null and you don’t want to have 4 characters null String, then you should use Objects.toString(i,null) to convert long to String.

Long i = null;
String longStr = Objects.toString(i,null);
System.out.println(longStr); //output null

Please note that java.util.Objects class is available from JDK 7.


Using String.valueOf()

You can use String’s valueOf method to convert long to String.

long i = 11L;
String longStr = String.valueOf(i);
System.out.println(longStr); //output '11'

Using + operator

You can use + operator to convert long to String.

long i = 12L;
String longStr = i+"";
System.out.println(longStr); //output '12'

Using String.format method()

You can also use String’s format() method to convert Long to String.
Here is the simple example.

long i = 13L;
String longStr = String.format("%d",i);
System.out.println(longStr); //output '13'

Using StringBuffer and StringBuilder

You can use StringBuffer and StringBuilder class to convert Long to String.

long i = 14L;
String longStr = new StringBuilder().append(i).toString();
System.out.println(longStr); //output '14'

Using DecimalFormat

You can use DecimalFormat class to convert Long to String.

long l = 132987654;
DecimalFormat decimalFormat = new DecimalFormat("#");
String longStr = decimalFormat.format(l);
System.out.println(longStr); // '132987654'

In case, you need to format String with ,, you can use below code.

long l = 132987654;
DecimalFormat decimalFormat = new DecimalFormat("#,##0");
String longStr = decimalFormat.format(l);
System.out.println(longStr); // '132,987,654'

Long to String with different Radix

We have seen all the examples with base 10. In case, you want to convert Long to String in different radix, you can use difference Long.toXXXString() method.

Binary

We can use Long’s toBinaryString() to convert Long to String in binary format.

long i = 12L;
String longStr = Long.toBinaryString(i);
System.out.println(longStr); //output '1100'

Octal

We can use Long’s toOctalString() to convert Long to String in Octal format.

long i = 12L;
String longStr = Long.toOctalString(i);
System.out.println(longStr); //output '14'

Hexadecimal

We can use Long’s toHexString() to convert Long to String in HexaDecimal format.

long i = 12L;
String longStr = Long.toHexString(i);
System.out.println(longStr); //output 'c'

Java program for Long to String Conversion

package org.arpit.java2blog.Java2blogPrograms;

import java.text.DecimalFormat;
import java.util.Objects;

public class LongToStringConversionMain {

    public static void main(String[] args) {

        long l1 = 22L;
        String longStr1 = Long.toString(l1);
        System.out.println("Using Long's toString(): "+longStr1); //output '22'

        Long l2 = null;
        String longStr2 = Objects.toString(l2,null);
        System.out.println("Using Object's toString(): "+longStr2); //output null

        long l3 = 11L;
        String longStr3 = String.valueOf(l3);
        System.out.println("Using String's valueOf(): "+longStr3); //output '11'

        long l4 = 12L;
        String longStr4 = l4+"";
        System.out.println("Using + operator: "+longStr4); //output '12'

        long l5 = 13L;
        String longStr5 = String.format("%d",l5);
        System.out.println("Using String's format(): "+longStr5); //output '13'

        long l6 = 132987654L;
        DecimalFormat decimalFormat = new DecimalFormat("#");
        String longStr6 = decimalFormat.format(l6);
        System.out.println("Using DecimalFormat: "+longStr6); // '132987654'

        long l7 = 132987654L;
        DecimalFormat decimalFormat1 = new DecimalFormat("#,##0");
        String longStr7 = decimalFormat1.format(l7);
        System.out.println("Using DecimalFormat: "+longStr7); // '132,987,654'

        long l11 = 14L;
        String longStr11 = new StringBuilder().append(l11).toString();
        System.out.println("Using StringBuilder: "+longStr11); //output '14'

        long l8 = 12L;
        String longStrBinary = Long.toBinaryString(l8);
        System.out.println("Binary: "+longStrBinary); //output '1100'

        long l9 = 12L;
        String longStrOctal = Long.toOctalString(l9);
        System.out.println("Octal: "+longStrOctal); //output '14'

        long l10 = 12L;
        String longStrHex = Long.toHexString(l10);
        System.out.println("Hex: "+longStrHex); //output 'c'
    }
}

Output:

Using Long’s toString(): 22
Using Object’s toString(): null
Using String’s valueOf(): 11
Using + operator: 12
Using String’s format(): 13
Using DecimalFormat: 132987654
Using DecimalFormat: 132,987,654
Using StringBuilder: 14
Binary: 1100
Octal: 14
Hex: c

That’s all about how to convert Long to String in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.