Example of Methods for Converting ong to String

Below is the implementation of the above methods:

Java




// Java program to convert Long to String
// using valueOf() Method
import java.util.*;
 
public class GFG {
 
    // main method
    public static void main(String args[])
    {
 
        // create a Long
        Long varLong = 999999999999L;
 
        System.out.println("Method 1: String.valueOf()");
 
        // convert into String
        String str = String.valueOf(varLong);
 
        // printing the type of str to
        // show that long has been converted to string
        System.out.println("Converted type : "
                           + str.getClass().getName()
                           + "\n");
 
        // *************************************************************************
        // //
 
        System.out.println("Method 2: Long.toString()");
        str = Long.toString(varLong);
 
        // printing the type of str to
        // show that long has been converted to string
        System.out.println("Converted type : "
                           + str.getClass().getName()
                           + "\n");
 
        // *************************************************************************
        // //
 
        System.out.println("Method 3: new Long(long l)");
        str = new Long(varLong).toString();
 
        // printing the type of str to
        // show that long has been converted to string
        System.out.println("Converted type : "
                           + str.getClass().getName()
                           + "\n");
 
        // *************************************************************************
        // //
 
        System.out.println("Method 4: String.format()");
        str = String.format("%d", varLong);
 
        // printing the type of str to
        // show that long has been converted to string
        System.out.println("Converted type : "
                           + str.getClass().getName());
 
        // *************************************************************************
        // //
 
        System.out.println(
            "Method 5: StringBuilder, StringBuffer object");
        str = new StringBuilder()
                  .append(varLong)
                  .toString();
 
        // printing the type of str to
        // show that long has been converted to string
        System.out.println("Converted type : "
                           + str.getClass().getName());
 
        System.out.println("Output: " + str);
    }
}


Output

Method 1: String.valueOf()
Converted type : java.lang.String

Method 2: Long.toString()
Converted type : java.lang.String

Method 3: new Long(long l)
Converted type : java.lang.String

Method 4: String.format()
Converted type : java.lang.String
Method 5: StringBuilder, StringBuffer object
Converted type : java.lang.String
Output: 999999999999


Java Program to Convert Long to String

The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String.

Given a Long number, the task is to convert it into a String in Java.

Examples of Long-to-String Conversion

Input: Long = 20L
Output: “20”

Input: Long = 999999999999L
Output: “999999999999”

Similar Reads

Java Program for long-to-String Conversion

For converting any data type to string type, we can simply add/+ empty string indicated by double quotes(“)....

Methods for Conversion from long to String in Java

...

Example of Methods for Converting ong to String

There are certain methods to Convert Long to String as mentioned below:...

Contact Us