8 ways to convert long to String in Java [Practical Examples]

Different methods to convert long to String in Java

In java, long is a primitive data type whereas Long is a wrapper class. As we know that java supports auto-boxing, so we can use long and Long interchangeably in most of the 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.

Advertisement
  • 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. Here, we are using a concatenation operator + to store the long value as a string.

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

// 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

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.

// 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

Advertisement
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.

// 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

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

 

Method-4: Using DecimalFormat class

In this approach, we are using the DecimalFormat class to convert long to String. Moreover, when we do not specify the format it takes the grouping separator comma(,) to separate the long numbers.

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.

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

Advertisement
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.

// 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

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 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.

// 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

Advertisement
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.

// 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

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.

// 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

Advertisement
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. 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

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X