×

How to Convert long to String in Java

How to Convert long to String in java

It is used if we have to display a long number in the text field because everything is displayed as a String. A long value can be converted into a String using any of the following methods:

  • Using String.valueOf() method
  • Using Long.toString() method
  • Using the Constructor of Long Class

Using String.valueOf(long l)

It is used to convert long to String. It is the static method of String class. This method takes long value as an argument and returns a string representation of it. The String.valueOf() is an overloaded method.The syntax of this method is-

public static String valueOf(long l)

Where l is the long value that is to be converted.

Example

Consider the following example; we have taken a long variable var having value 7823597L, where l denotes the long type. The String is a class. str is a variable that stores the converted value of var. The valueOf() is a built-in method of String class passing a variable var as an argument. The println statement prints the converted value of var.

public class LongToStringExample
{
public static void main(String[] args)
{
long var = 7823597L;
String str = String.valueOf(var);
System.out.println("Converted String is: "+str);
}
}

Output

Converted String is: 7823597

Using Long.toString(long l)

This method works same as String.valueOf() method. It returns the String that represents the long value that we pass to this method. This is the most common and popular method for converting a long to String. The sign will be preserved if the number is negative. The syntax of this method is-

public static StringtoString(long l)

Where l is the long value, we are passing to the method as an argument.

Example

Consider the following example, here we have taken variable num of type long having value 999999999L. The String is a class. str is a variable that stores the converted value of num. Long is a wrapper class. The toString() is a built-in method passing an argument num. The println statement prints the converted value of num.

public class LongToStringExample1
{
public static void main(String[] args)
{
long num = 999999999L;
String str = Long.toString(num);
System.out.println("Converted String is: "+str);
}
}

Output

Converted String is: 999999999

Using newLong(long).toString()

An alternative method is to create an instance of the Long class and then invoke it's toString() method. It is a little less efficient than the first two methods shown above. Before conversion is performed, a new instance of the Long class is created.

The use of Long.toString() and String.valueOf() are ideal if the variable you want to convert is primitive (long). But if your variable is already an instance of Long (wrapper class of the primitive type long), it is better to just invoke its toString() method as shown in the code above. We can create a constructor in the following forms-

long number = -782L;
Long longInstance = new Long(number);      
String numberAsString = longInstance.toString();

We can also write the above code as: 

long number = -782L;
String numberAsString = new Long(number).toString();

or

String numberAsString = new Long(-1235).toString();

Example 

In the following example; we have taken a variable val of type long having value 98757563L. String is a class. str is a variable that stores the converted value of num. The new keyword creates the constructor of Long class having val as an argument. The toString() is a built-in method. The println statement prints the converted value of val.

public class LongToStringExample
{
public static void main(String[] args)
{
long val = 98757563L;
String str= new Long(val).toString(); //Conversion using constructor of long class
System.out.println("After Conversion: " +str);
}
}

Output

After Conversion: 98757563

Related Topics

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.