Java Convert Long to String Example

In this article, we will show you how to convert Java wrapper class Long or primitive type long to String code examples. Performing conversion from Long to String is a common scenario when programming with Java.
In this article, we will show you 7 different ways we can do convert Java wrapper class Long or primitive type long to String.
  1. Convert using Long.toString(long)
  2. Convert using String.valueOf(long)
  3. Convert using new Long(long).toString()
  4. Convert using String.format()
  5. Convert using DecimalFormat
  6. Convert using StringBuffer
  7. Convert using StringBuilder

1. Convert using Long.toString(long)

/ Convert using Long.toString(int)
long number = 100L;
String numberAsString = Long.toString(number);
System.out.println("Convert using Long.toString(int) -> " + numberAsString);
Output:
Convert using Long.toString(int) -> 100

2. Convert using String.valueOf(long)

// Convert using String.valueOf(int)
long number = 100L;
String numberAsString1 = String.valueOf(number);
System.out.println("Convert using String.valueOf(int) -> " + numberAsString1);
Output:
Convert using String.valueOf(int) -> 100

3. Convert using new Long(long).toString()

// Convert using new Integer(int).toString()
long number = 100L;
Long longInstance = new Long(number);
String numberAsString2 = longInstance.toString();
System.out.println("Convert using new Long(int).toString() -> " + numberAsString2);
Output:
Convert using new Long(int).toString() -> 100

4. Convert using String.format()

// Convert using String.format()
long number = 100L;
String numberAsString3 = String.format("%d", number);
System.out.println("Convert using String.format() -> " + numberAsString3);
Output:
Convert using String.format() -> 100

5. Convert using DecimalFormat

// Convert using DecimalFormat
long number1 = 12345L;
DecimalFormat decimalFormat = new DecimalFormat("#");
String numberAsString4 = decimalFormat.format(number1);
System.out.println(numberAsString);
System.out.println("Convert using DecimalFormat -> " + numberAsString4);
Output:
Convert using DecimalFormat -> 12345

6. Convert using StringBuffer

// Convert using StringBuffer Example
long number = 100L;
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(number);
String numberAsString5 = stringBuffer.toString();
System.out.println("Convert using StringBuffer Example -> " + numberAsString5);
Output:
Convert using StringBuffer Example -> 100

7. Convert using StringBuilder

// Convert using StringBuilder Example
long number = 100L;
StringBuilder sbBuilder = new StringBuilder();
sbBuilder.append(number);
String numberAsString6 = sbBuilder.toString();
System.out.println("Convert using StringBuilder Example  -> " + numberAsString6);
Output:
Convert using StringBuilder Example  -> 100

Complete Program for Reference

import java.text.DecimalFormat;

/**
 * ConvertLongToStringExample
 * @author javaguides.net
 *
 */
public class ConvertLongToStringExample {
 public static void main(String[] args) {
  
  // Convert using Long.toString(int)
  long number = 100L;
  String numberAsString = Long.toString(number);
  System.out.println("Convert using Long.toString(int) -> " + numberAsString);

  // Convert using String.valueOf(int)
  String numberAsString1 = String.valueOf(number);
  System.out.println("Convert using String.valueOf(int) -> " + numberAsString1);

  // Convert using new Integer(int).toString()
  Long longInstance = new Long(number);
  String numberAsString2 = longInstance.toString();
  System.out.println("Convert using new Long(int).toString() -> " + numberAsString2);

  // Convert using String.format()
  String numberAsString3 = String.format("%d", number);
  System.out.println("Convert using String.format() -> " + numberAsString3);

  // Convert using DecimalFormat
  long number1 = 12345L;
  DecimalFormat decimalFormat = new DecimalFormat("#");
  String numberAsString4 = decimalFormat.format(number1);
  System.out.println(numberAsString);
  System.out.println("Convert using DecimalFormat -> " + numberAsString4);

  // Convert using StringBuffer Example
  StringBuffer stringBuffer = new StringBuffer();
  stringBuffer.append(number);
  String numberAsString5 = stringBuffer.toString();
  System.out.println("Convert using StringBuffer Example -> " + numberAsString5);

  // Convert using StringBuilder Example
  StringBuilder sbBuilder = new StringBuilder();
  sbBuilder.append(number);
  String numberAsString6 = sbBuilder.toString();
  System.out.println("Convert using StringBuilder Example  -> " + numberAsString6);

 }
}
Output:
Convert using Long.toString(int) -> 100
Convert using String.valueOf(int) -> 100
Convert using new Long(int).toString() -> 100
Convert using String.format() -> 100
100
Convert using DecimalFormat -> 12345
Convert using StringBuffer Example -> 100
Convert using StringBuilder Example  -> 100

Related Java String Conversion Examples

Comments