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.
Table of Contents
Using Long.toString()
You can use Long class toString()
method to convert long to String.
1 2 3 4 5 |
long i = 22L; String longStr = Long.toString(i); System.out.println(longStr); //output '22' |
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.
1 2 3 4 5 |
Long i = null; String longStr = Objects.toString(i,null); System.out.println(longStr); //output null |
Using String.valueOf()
You can use String’s valueOf
method to convert long to String.
1 2 3 4 5 |
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.
1 2 3 4 5 |
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.
1 2 3 4 5 |
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.
1 2 3 4 5 |
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.
1 2 3 4 5 6 |
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.
1 2 3 4 5 6 |
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.
1 2 3 4 5 |
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.
1 2 3 4 5 |
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.
1 2 3 4 5 |
long i = 12L; String longStr = Long.toHexString(i); System.out.println(longStr); //output 'c' |
Java program for Long to String Conversion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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 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.