8 ways to convert long to String in Java

Short on time?Beta Get the gist of this 7-minute read in seconds, then ask follow-up questions

For broader language coverage, see the Java tutorial index. For more Python walkthroughs, open the Python tutorial index.

Different methods to convert long to String in Java

In Java, long is a primitive data type whereas Long is a wrapper class—the same boxing model as int and Integer in difference between Integer and int. Auto-boxing lets you use long and Long interchangeably in most cases.

In several applications, we may need to convert a long number to a string because we need to operate on the value in its string form. In many GUI based application also, if we have to display long value in text field we may need to convert it before displaying. There are several ways to convert long type data to a string. The list below shows eight different ways in which we can convert long to string.

  • Using + operator
  • Using String.valueOf()
  • Using Long.toString()
  • Using DecimalFormat class
  • Using String.format()
  • Using StringBuilder class
  • Using StringBuffer class
  • Using Long constructor

Method-1: Using + operator

This is the simplest approach for converting long to a string. Concatenating a long with an empty string relies on the same + coercion rules as Java string concatenation.

Example : In this example, we concatenate blank string "" with long variable l.

java
// Program to convert long to string in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variable
        long l = 8584157;

        // Converting long to string
        String s = l + "";

        // Printing both the variables
        System.out.println("The value of long variable is " + l);
        System.out.println("The value of String variable is " + s);
    }
}
Output

Output

The value of long variable is 8584157
The value of String variable is 8584157

Method-2: Using String.valueOf()

In this approach, we are using valueof method of String class that converts long values passed as a parameter into a string.

Example : In this example, long variable l is converted to string using valueOf method.

java
// Program to convert long to string in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variable
        long l = 1587421;

        // Converting long to string
        String s = String.valueOf(l);

        // Printing both the variables
        System.out.println("The value of long variable is " + l);
        System.out.println("The value of String variable is " + s);
    }
}
Output

Output

The value of long variable is 1587421
The value of String variable is 1587421

Method-3: Using Long.toString()

In this approach, we are using Long wrapper class to convert long value to a string. Each of the Number subclasses includes a class method, toString(), that will convert its primitive type to a string.

**Example :**In this example, we are using toString method of Long class for conversion.

java
// Program to convert long to string in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variable
        long l = 1598521;

        // Converting long to string
        String s = Long.toString(l);

        // Printing both the variables
        System.out.println("The value of long variable is " + l);
        System.out.println("The value of String variable is " + s);
    }
}
Output

Output

The value of long variable is 1598521
The value of String variable is 1598521

Method-4: Using DecimalFormat class

In this approach, we use the DecimalFormat class to convert long to String with locale-aware grouping; see Java DecimalFormat for pattern symbols beyond the default comma separator.

**Example :**In this example, we are using format method of DecimalFormat class for conversion. Here, for one of the string we are not giving any specific format. So, by default it will take comma as a separator.

java
import java.text.DecimalFormat;
// Program to convert long to string in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variable
        long l = 1598753;

        // Converting long to string
        DecimalFormat df = new DecimalFormat();
        String s1 = df.format(l);

        df = new DecimalFormat("#");
        String s2 = df.format(l);

        // Printing both the variables
        System.out.println("The value of long variable is " + l);
        System.out.println("The value of String variable s1 is " + s1);
        System.out.println("The value of String variable s2 is " + s2);
    }
}
Output

Output

The value of long variable is 1598753
The value of String variable s1 is 1,598,753
The value of String variable s2 is 1598753

Method-5: Using String.format()

In this approach, we are using format method of a String class to convert long to string.

Example : In this example, we will use the access specifier "%d" as the value to format is a long type.

java
// Program to convert long to string in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variable
        long l = 1597532;

        // Converting long to string
        String s = String.format("%d", l);

        // Printing both the variables
        System.out.println("The value of long variable is " + l);
        System.out.println("The value of String variable is " + s);
    }
}
Output

Output

The value of long variable is 1597532
The value of String variable is 1597532

Method-6: Using StringBuilder class

In this approach, we are using append method of StringBuilder class. The append() method adds the string representation of the long argument to the builder, then toString() returns the final String—the same builder pattern used for multi-part concatenation.

Example : In this example, we will use the append() method to add long value and toString() method to convert it to string.

java
// Program to convert long to string in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variable
        long l = 1598753;

        // Converting long to string
        StringBuilder b = new StringBuilder();
        String s = b.append(l).toString();

        // Printing both the variables
        System.out.println("The value of long variable is " + l);
        System.out.println("The value of String variable is " + s);
    }
}
Output

Output

The value of long variable is 1598753
The value of String variable is 1598753

Method-7: Using StringBuffer class

In this approach, we are using append method of StringBuffer class. The append() method is used to append the string representation of the long argument to this sequence.

Example : In this example, we will use the append() method to add long value and toString() method to convert it to string.

java
// Program to convert long to string in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variable
        long l = 1598753;

        // Converting long to string
        StringBuffer b = new StringBuffer();
        String s = b.append(l).toString();

        // Printing both the variables
        System.out.println("The value of long variable is " + l);
        System.out.println("The value of String variable is " + s);

    }
}
Output

Output

The value of long variable is 1598753
The value of String variable is 1598753

Method-8: Using Long Constructor

In this approach, we are using constructor of Long class to initialize with long value and then use toString() method to convert Long object to the string. Although, this approach is deprecated from Java version 9, it can be used in the older versions.

Example : In this example, we will use the constructor to initialize the object with value and toString() method to convert it to string.

java
// Program to convert long to string in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variable
        long l = 1598753;

        // Converting long to string
        Long o = new Long(l);
        String s = o.toString();

        // Printing both the variables
        System.out.println("The value of long variable is " + l);
        System.out.println("The value of String variable is " + s);

    }
}
Output

Output

The value of long variable is 1598753
The value of String variable is 1598753

Summary

The knowledge of converting long value to a String in Java is very useful while working on real time applications. In this tutorial, we covered eight different approaches to convert long to String in Java. For double values, the same API families apply in convert double to String in Java. As per the requirement of an application, we can choose an appropriate approach for conversion. We learned in detail about this approaches with an example.

All in all, this tutorial, covers everything that you need to know in order to have a clear view on conversion of long value to a string object in Java.


References

Converting between Numbers and Strings
DecimalFormat Class
Strings Class
StringBuilder Class
StringBuffer Class

Finished reading?Beta Get the key takeaways in seconds, then ask follow-up questions
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with 15+ years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)