Java Program to Convert long to String

In the previous article we have discussed Java Program to Convert int to Char

In this article we will see how to convert a long to string.

Program To Convert long To String

Before going into the program , let’s see some examples of both long and String type.

Example-1: String types

String a = "b";
String b = "5643646442";
Examples-2: Long types

long a = 56666L;
long b = 5643646442;

Let’s see different ways to convert long to String.

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Method 1 : Java Program to Convert long to String Using valueOf() method

long can be converted to string by using valueOf() , let’s see how it works.

String.valueOf() is a method  which will simply typecasts below-given parameter to strings always. It is an inbuilt method of String class in java.

Approach :

  1. Take a long value and store it in a long variable input1.
  2. Then pass that input1 variable as parameter to String.valueOf( ) method which will convert the long to string value and return it .
  3. Store that string value in a variable output.
  4. Display the result .

Program :

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a long value through scanner class
        System.out.print("Enter a long value : ");
        long input1=sc.nextLong();
        // converting to string
        String output = String.valueOf(input1);
        System.out.println("Converted String is : "+output);
    }
}
Output : 

Enter a Long value : 6567876
Converted String is :6567876

Method 2 :  Java Program to Convert long to String Using toString() method

long can be converted to string by using  toString() method,  let’s see how it actually works.

Whenever we use print statement in java,  toString() method of Object class in java is always called. toString() method of Object Class in java is always called by directly or indirectly. Here we are using this directly to convert the long to string itself .

Approach :

  1. Take a long value and store it in a long variable input1.
  2. Then pass that input1 variable as parameter to Long.toString( ) method which will convert the long to string value and return it .
  3. Store that string value  in a variable output.
  4. Display the result.

Program :

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a long value through scanner class
        System.out.print("Enter a Long value : ");
        long input1=sc.nextLong();
        // converting to String
        String output = Long.toString(input1);
        System.out.println("Converted String is :"+output);
    }
}
Output : 

Enter a Long value : 6567876
Converted String is :6567876

Method 3 : Java Program to Convert long to String Using  “+” operator

long can be converted to string by using  “+” operator ,  let’s see how it actually works.

Approach :

  1. Take a long value and store it in a long variable input1.
  2. Take a String variable and concatenate “+” with the input variable which will be treated as string.
  3. Display the result.

Program :

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a long value through scanner class
        System.out.print("Enter a Long value : ");
        long input1=sc.nextLong();
        // converting to String
        String output = " " + input1;
        System.out.println("Converted String is :"+output);
    }
}
Output : 

Enter a Long value : 6567876
Converted String is :6567876

Method 4 : Java Program to Convert long to String Using format() method

long can be converted to string by using  format() method, let’s see how it actually works.

Approach :

  1. Take a long value and store it in a Integer variable input1.
  2. Then pass that input1 variable as parameter to String.format( ) method which will convert the long to string value and return it .
  3. Store that string value  in a variable output.
  4. Display the result .

Program :

import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a long value through scanner class
        System.out.print("Enter a Long value : ");
        long input1=sc.nextLong();
        // converting to String
        String output =String.format("%d", input1);;
        System.out.println("Converted String is : "+output);
   }
}
Output : 

Enter a Long value : 6567876
Converted String is :6567876

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java
programs with the help of the Java basic programs list available

Related Java Program: