How to Convert Long to String in Java

In this tutorial, we will learn how to convert primitive long or wrapper Long class object to string in Java.

Sometimes, we need to convert values from one data type to another, under certain situation.

For example, if we need to append a long value to a JTextArea, first we need to convert it to a string before appending to JTextArea.

Therefore, this tutorial will cover seven different ways to convert a primitive long or Long wrapper class to string in Java.

Different ways for converting long to string are as:

  • Convert using Long.toString(long)
  • Convert using String.valueOf(long)
  • Convert using new Long(long).toString()
  • Convert using String.format()
  • Convert using DecimalFormat
  • Convert using StringBuffer
  • Convert using StringBuffer

Let’s understand all the above ways one by one with example programs.

How to convert long to string in Java

Converting Long to String in Java using String.valueOf()


To convert primitive long to string in Java, we use valueOf() method of the String class. The method String.valueOf() converts long to string. It is a static utility method of the String class.

Therefore, we do not need to create an object to calling this method. We can simply call by using its class name. The general signature of valueOf() method is as below:

public static String valueOf(long l)

This method accepts a long numeric value that needs to be converted and returns the string representation of the long value. Let’s create a simple program to convert a primitive long to string value in Java.

Program code 1:

// Java Program to demonstrate the conversion of long into string using String.valueOf().
package javaConversion;  
public class LongToStringConversion {
public static void main(String[] args) 
{
 long num = 5000L;	
// Call valueOf() method of String class for converting long to string. 
   String str = String.valueOf(num);  
   System.out.println(num + 200); // 700 because + is binary plus operator .
   System.out.println(str + 200);// 500200 because + is string concatenation operator.
 }
}
Output:
      5200
      5000200

Converting Long to String in Java using Long.toString(long)


The long wrapper class in Java also provides a toString() method that converts long numeric value to String. The toString() method of Long class is a static utility method. It returns a string object representing the specified long numeric value.

The general signature of this method is as below:

public static String toString(long l)

Let’s create a simple Java program to convert a primitive long to string in java using Long.toString() method.

Program code 2:

// Java Program to demonstrate the conversion of long into string using Long.toString().
package javaConversion;  
public class LongToStringConversion {
public static void main(String[] args) 
{
 long num = 5000L;	
// Call toString() method of Long class for converting long to string. 
   String str = Long.toString(num);
   System.out.println(num + 200); // 700 because + is binary plus operator .
   System.out.println(str + 200);// 500200 because + is string concatenation operator.
 }
}
Output:
      5200
      5000200

Converting Long to String using new Long(long).toString()


We can also convert long numeric value to string by creating Long class object and then invoking its toString() method. Look at the below example program based on it.

Program code 3:

// Java Program to demonstrate the conversion of long into string using new Long(num).toString().
package javaConversion;  
public class LongToStringConversion {
public static void main(String[] args) 
{
 long num = 5000L;	
 String str = new Long(num).toString();  
 System.out.println(num + 200); // 700 because + is binary plus operator .
 System.out.println(str + 200);// 500200 because + is string concatenation operator.
 }
}
Output:
      5200
      5000200

Convert Long to String using String.format()


The String.format() method of String class formats given arguments into String. Java has introduced it since JDK 1.5. This is a new alternative way for converting a Long class object to a string object.

The purpose of this method is to format a string. The general syntax for this method is as below:

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

Let’s create a Java program to convert a primitive long to string in Java using format() method of String class.

Program code 4:

// Java Program to demonstrate the conversion of long numeric value into string using format().
package javaConversion;  
public class LongToStringConversion {
public static void main(String[] args) 
{
 long num = 5000L;	
 String str = String.format("%d", num);  
 System.out.println(str); 
 }
}
Output:
      5000

Converting Long to String using DecimalFormat


DecimalFormat is a concrete subclass of NumberFormat class that formats a number to a string representation. It provides a lot of features designed to parse and format numbers.

We can use it to format a number to a string representation using a certain pattern. Here’s an example program using DecimalFormat.

Program code 5:

// Java Program to demonstrate the conversion of long into string using DecimalFormat.
package javaConversion;
import java.text.DecimalFormat;
public class LongToStringConversion {
public static void main(String[] args) 
{
 long num = 5000L;	
 DecimalFormat numberFormat = new DecimalFormat("##,###");
 String str = numberFormat.format(num);        
 System.out.println("Number to be converted is: " + num);
 System.out.println("String representation of 5000 is: " + str);
 }
}
Output:
      Number to be converted is: 5000
      String representation of 5000 is: 5,000

Convert Long to String in Java using StringBuffer


StringBuffer is a class that concatenates multiple values into a single string. It is thread-safe, but it is slower.

The StringBuffer object represents a String object that we can change and treat as an array with a sequence of characters.

To add a new value to the end of the string, StringBuffer instance implements the append() method. At the end, call the toString() method, in order to take the string representation of the data.

Here is an example program based on the conversion of long to String in Java.

Program code 6:

// Java Program to demonstrate the conversion of long into string using StringBuffer.
package javaConversion;
public class LongToStringConversion {
public static void main(String[] args) 
{
 long num = 5000L;	
// Create StringBuffer object.  
  StringBuffer sb = new StringBuffer();
  sb.append(num);
  String str = sb.toString();
  System.out.println("String representation: " + str);
 }
}
Output:
      String representation: 5000

You can also write the following code to converting a primitive long type to string in Java.

String str = new StringBuffer().append(5000).toString();
System.out.println("String representation: " + str);

Converting Long to String using StringBuilder


Let’s create a Java program to convert long numeric value to string representation using StringBuilder class. We can change (or modify) the content of StringBuilder, without creating a new StringBuilder object.

Program code 7:

// Java Program to demonstrate the conversion of long into string using StringBuilder.
package javaConversion;
public class LongToStringConversion {
public static void main(String[] args) 
{
 long num = 5000L;	
// Create StringBuffer object.  
  StringBuffer sb = new StringBuffer();
  sb.append(num);
  String str = sb.toString();
  System.out.println("String representation: " + str);
 }
}
Output:
     String representation: 5000

In this tutorial, you learned how to convert a primitive type long to string in Java in several ways. Hope that you will have understood and practiced all programs based on the conversion of long to string in Java.
Thanks for reading!!!
Next ⇒ Convert String to Float in Java⇐ Prev Next ⇒