JavaDevNotes.com

Java Long To String Examples

This tutorial will show example codes on how to convert Java Long to String. Performing conversion from long to String is a common scenario when programming with Core Java.
Here are some quick reference example codes:

Convert using Long.toString(long)

The Long class has a static method that returns a String object representing the specified long parameter. This is a very efficient solution.

Syntax

public static String toString(long i)

The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved.

Example
long number = -782;
String numberAsString = Long.toString(number);
The code is equivalent to:
String numberAsString = "-782";
This is the most common and popular solution for converting a long to String.


Convert using String.valueOf(long)

The String class has several static methods that can convert most primitive types to their String representation. This includes the long data type.

Example

long number = -782;
String numberAsString = String.valueOf(number);
or
String numberAsString = String.valueOf(-782);

This is also a very simple and popular solution for converting a long to String. This is also as efficient as the first solution shown above.


Convert using new Long(long).toString()

Another alternative method is to create an instance of Long class and then invoke it's toString() method. This is a little less efficient than the first two options shown above. This is because a new instance of Long is created before conversion is performed.

Example

long number = -782;
Long longInstance = new Long(number);      
String numberAsString = longInstance.toString();

We can shortened to:

long number = -782;
String numberAsString = new Long(number).toString();

or just:

String numberAsString = new Long(-782).toString();

Using Long.toString(long) and String.valueOf(long) are ideal if the variable you want to convert is primitive (long). But if your variable is already an instance of Long (wrapper class of the primitive type long), it is better to just invoke it's toString() method as shown in the code above.


Convert using String.format()

String.format() is a new alternative that can be used for converting a Long to String object. It is first introduced in Java 5 (JDK1.5) and has many cool features.

Syntax
public static String format(String format, Object... args)

There are many options on how to use this method. But for conversion purposes, here is a simple example:

Example
long number = -782;
String numberAsString = String.format ("%d", number);
And the variable numberAsString will have the value "-782"

If you are using Java 5 or higher, this is also a very simple alternative for converting a long to String object.



Convert using DecimalFormat

The class java.text.DecimalFormat is a class that formats a number to a String representation following a certain pattern.
Example

long number = 1234567890;
DecimalFormat decimalFormat = new DecimalFormat("#");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output
1234567890

This is an example that will convert to String but with commas

long number = 1234567890;
DecimalFormat decimalFormat = new DecimalFormat("#,##0");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output
1,234,567,890

If your requirements needs control on the formatting of the resulting String, this solution is very ideal. You can specify comma separator for readability and control the number of decimal places.


Convert using StringBuffer or StringBuilder

StringBuffer is a class that is used to concatenate multiple values into a String. StringBuilder works similarly but not thread safe like StringBuffer. These two classes can be used to convert a Java Long to String.

StringBuffer Example

long number = -782;
StringBuffer sb = new StringBuffer();
sb.append(number);
String numberAsString = sb.toString();

StringBuilder Example

long number = -782;
StringBuilder sb = new StringBuilder();
sb.append(number);
String numberAsString = sb.toString();

Shortened Examples

String numberAsString = new StringBuffer().append(-782l).toString();

String numberAsString = new StringBuilder().append(-782l).toString();
Putting an l at the end of a number signifies that it is a literal long. E.g. 500l instead of just specifying 500.


Quick Solution

If you want something simple that works but not elegant, you can use this code:
long number = -782;
String numberAsString = "" + number;

When you concatenate a String and an Long, the result is a String. Internally, the code is inefficient because there are intermediate objects that are created behind the scene.


Convert with special radix

All of the examples above uses the base (radix) 10. There are cases when we wish to convert a Java Long to String but using another base. There are convenient methods to convert to binary, octal, and hexadecimal system. Arbitrary custom number system is also supported.

Examples

  • Binary
    long number = 65535;
    String binaryString = Long.toBinaryString(number);
    System.out.println(binaryString);
    
    The output is
    1111111111111111
    
    1111111111111111 is the binary representation of the number 65535.
  • Octal
    long number = 65535;
    String octalString = Long.toOctalString(number);
    System.out.println(octalString);
    
    The output is
    177777
    
    177777 is the octal representation of the number 65535.
  • Hexadecimal
    long number = 65535;
    String hexString = Long.toHexString(number);
    System.out.println(hexString);
    
    The output is
    ffff
    
    ffff is the hexadecimal representation of the number 65535.
  • Custom Base/Radix
    We can use any other custom base/radix when converting a Long to String. The sample shown below uses the base 7 number system.
    long number = 65535;
    String customString = Long.toString(number, 7);
    System.out.println(customString);
    
    The output is
    362031
    
  • 362031 is the representation of the number 65535 when written in the base 7 system.

String Tutorials And Examples