×

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

Session Tracking in Java

When a series of requests from the same User (i.e., requests coming from the same browser) occurs over an extended period of time, servlets employ a mechanism known as session...

3 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Self-Descriptive Numbers in Java

A number n is given. Identifying the self-descriptive numbers that exist between 1 and n is our task. Self-Descriptive Numbers The definition of a self-descriptive number, m, is a number with b...

6 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.