Java Program to Convert Long to String (original) (raw)

Last Updated : 26 Dec, 2023

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”

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(“).

**Syntax:

String str = l+" ";

Below is the implementation of the above method:

Java

public class GFG {

`` public static void main(String args[])

`` {

`` Long varLong = 999999999999L;

`` String str = varLong + " " ;

`` System.out.println( "Converted type : "

`` + str.getClass().getName());

`` System.out.println(str);

`` }

}

Output

Converted type : java.lang.String 999999999999

Methods for Conversion from long to String in Java

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

Method Description Syntax
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. String str = String.valueOf(varLong);
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. String str = Long.toString(varLong);
new Long(long l) This constructor is **not valid in Java 9. String str = new Long(varLong).toString();
String.format() The **java string format() method returns a formatted string using the given **locale, specified **format string and **arguments. String str = String.format(“%d”, varLong);
Using StringBuilder, StringBuffer object StringBufferis 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. String str = new StringBuilder().append(varLong).toString();

Example of Methods for Converting ong to String

Below is the implementation of the above methods:

Java

import java.util.*;

public class GFG {

`` public static void main(String args[])

`` {

`` Long varLong = 999999999999L;

`` System.out.println( "Method 1: String.valueOf()" );

`` String str = String.valueOf(varLong);

`` System.out.println( "Converted type : "

`` + str.getClass().getName()

`` + "\n" );

`` System.out.println( "Method 2: Long.toString()" );

`` str = Long.toString(varLong);

`` System.out.println( "Converted type : "

`` + str.getClass().getName()

`` + "\n" );

`` System.out.println( "Method 3: new Long(long l)" );

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

`` System.out.println( "Converted type : "

`` + str.getClass().getName()

`` + "\n" );

`` System.out.println( "Method 4: String.format()" );

`` str = String.format( "%d" , varLong);

`` System.out.println( "Converted type : "

`` + str.getClass().getName());

`` System.out.println(

`` "Method 5: StringBuilder, StringBuffer object" );

`` str = new StringBuilder()

`` .append(varLong)

`` .toString();

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

Similar Reads

Java Basic Programs





















Java Pattern Programs














Java Conversion Programs















Java Classes and Object Programs
















Java Methods Programs











Java Searching Programs




Java 1-D Array Programs













Java 2-D Arrays (Matrix) Programs













Java String Programs