All for Joomla All for Webmasters

Java long to String

We can convert long to String in java using String.valueOf() and Long.toString() methods.

Scenario

It is generally used if we have to display long number in textfield because everything is displayed as a string in form.

5


1) String.valueOf()

The String.valueOf() is an overloaded method. It can be used to convert long to String. The valueOf() is the static method of String class. The signature of valueOf() method is given below:

  1. public static String valueOf(long i)

Java long to String Example using String.valueOf()

Let’s see the simple code to convert int to String in java.

  1. long i=9993939399L;//L is the suffix for long
  2. String s=String.valueOf(i);//Now it will return “9993939399”

Let’s see the simple example of converting String to int in java.

  1. public class LongToStringExample1{
  2. public static void main(String args[]){
  3. long i=9993939399L;
  4. String s=String.valueOf(i);
  5. System.out.println(s);
  6. }}

 

Output:

9993939399

2) Long.toString()

The Long.toString() method converts long to String. The toString() is the static method of Long class. The signature of toString() method is given below:

  1. public static String toString(long i)

Java long to String Example using Long.toString()

Let’s see the simple code to convert int to String in java using Long.toString() method.

  1. long i=9993939399L;
  2. String s=Long.toString(i);//Now it will return “9993939399”

Let’s see the simple example of converting String to int in java.

  1. public class LongToStringExample2{
  2. public static void main(String args[]){
  3. long i=9993939399L;
  4. String s=Long.toString(i);
  5. System.out.println(s);
  6. }}

Output:

9993939399
PinIt
submit to reddit

Leave a Reply

Top