Open In App
Related Articles

Java Program to Convert Long to String

Improve Article
Improve
Save Article
Save
Like Article
Like

Conversion of long type to string type generally comes in need in the case when we have to display a long number in GUI application because everything is displayed as string form

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

Examples of Long to String Conversion

Input: Long = 20L
Output: “20”

Input: Long = 999999999999L
Output: “999999999999”

Methods for Conversion from Long to String

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

  1. Using + operator
  2. Using String.valueOf()
  3. Using Long.toString()
  4. new Long(long l)
  5. Using String.format()
  6. Using StringBuilder, StringBuffer object

1. Using + operator

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

Syntax:

String str = l+" ";

Below is the implementation of the above method:

Java




// Java program to convert Long to String  
// using + operator 
      
public class GFG {  
      
    // main method  
    public static void main(String args[])  
    {  
      
        // create a Long  
        Long varLong = 999999999999L;  
      
        // convert into String 
        String str = varLong+" "
        
        // printing the type of str to
        // show that long has been converted to string
        System.out.println("Converted type : "+str.getClass().getName());
      
        // print Long as String  
        System.out.println(str);  
    }  
}


Output

Converted type : java.lang.String
999999999999

2. Using String.valueOf()

The valueOf() method converts data from its internal form into human-readable form. It is a static method that is overloaded within a string for all of Java’s built-in types so that each type can be converted properly into a string.
It is called when a string representation of some other type of data is needed-for example during concatenation operation. You can call this method with any data type and get a reasonable String representation.

Syntax:

String str = String.valueOf(varLong);

Below is the implementation of the above method:

Java




// Java program to convert Long to String
// using valueOf() Method
  
public class GFG {
  
    // main method
    public static void main(String args[])
    {
  
        // create a Long
        Long varLong = 999999999999L;
  
        // 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());
  
        // print Long as String
        System.out.println(str);
    }
}


Output

Converted type : java.lang.String
999999999999

To know more about the method refer to the String.valueOf() article.

3. Using Long.toString()

Object class contains toString() method. We can use toString() method to get string representation of an object. Whenever we try to print the Object reference then internally toString() method is invoked. If we did not define toString() method in your class then Object class toString() method is invoked otherwise our implemented/Overridden toString() method will be called.

Syntax:

String str = Long.toString(varLong);

Below is the implementation of the above method:

Java




// Java program to convert Long to String  
// using toString() method
      
public class GFG {  
      
    // main method  
    public static void main(String args[])  
    {  
      
        // create a Long  
        Long varLong = 999999999999L;  
      
        // convert into String 
        String 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());
        
        // print Long as String  
        System.out.println(str);  
    }  
}


Output

Converted type : java.lang.String
999999999999

To know more about the method refer to the toString() article.

4. new Long(long l)

This constructor is not valid in Java 9.

Syntax:

String str = new Long(varLong).toString();

Below is the implementation of the above method:

Java




// Java program to convert Long to String  
// using long constructor  
   
import java.util.*;
public class GFG {  
      
    // main method  
    public static void main(String args[])  
    {  
      
        // create a Long  
        Long varLong = 999999999999L;  
      
        // convert into String 
        String 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());
        
        // print Long as String  
        System.out.println(str);  
    }  
}


 
Since this version of Java does not support this constructor. Hence, on running the program, we will get the error displaying

Note: prog.java uses or overrides a deprecated API.

5. Using String.format()

The java string format() method returns a formatted string using the given locale, specified format string and arguments.

Syntax:

long varLong = 9999999L;
String str = String.format("%d", varLong); 

Below is the implementation of the above method:

Java




// Java program to demonstrate
// working of format() method
  
class Gfg {
    
    public static void main(String args[])
    {
        long varLong = 9999999L;
  
        String 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());
  
        // print Long as String
        System.out.println(str);
    }
}


Output

Converted type : java.lang.String
9999999

To know more about the method refer to the String.format() article.

6. Using StringBuilder, StringBuffer object

StringBuffer is a peer class of String that provides much of the functionality of strings. The string represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences.

Syntax:

long varLong = 9999999L;
String str = new StringBuilder().append(varLong).toString();

Below is the implementation of the above method:

Java




// Java program to convert long to
// string using StringBuilder
  
class Gfg {
    public static void main(String args[])
    {
        long varLong = 9999999L;
  
        String 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());
  
        // print Long as String
        System.out.println(str);
    }
}


Output

Converted type : java.lang.String
9999999

To know more about StringBuilder and StringBuffer classes refer to these links.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 Aug, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials