Write a Java program to convert long to string. We can use valueOf and toString() methods to convert long to string. This example accepts the user input long value and converts it to a string using the valueOf function.
package NumPrograms; import java.util.Scanner; public class longToString1 { private static Scanner sc; public static void main(String[] args) { sc= new Scanner(System.in); System.out.print("Enter Any Long Value = "); long l1 = sc.nextLong(); String s1 = String.valueOf(l1); System.out.println("Long To String = " + s1); } }
This program helps to convert long to string using the toString method.
package NumPrograms; import java.util.Scanner; public class longToString2 { private static Scanner sc; public static void main(String[] args) { sc= new Scanner(System.in); System.out.print("Enter Any Long Value = "); long l1 = sc.nextLong(); String s1 = Long.toString(l1); System.out.println("Long To String = " + s1); } }
Enter Any Long Value = 9877263723
Long To String = 9877263723
As we mentioned before, there are two possibilities to convert long data type to string and they are valueOf & toString. Let me use both the available options valueOf & toString to convert long to string in one example.
package NumPrograms; import java.util.Scanner; public class longToString3 { private static Scanner sc; public static void main(String[] args) { sc= new Scanner(System.in); System.out.print("Enter Any Long Value = "); long l1 = sc.nextLong(); String s1 = String.valueOf(l1); String s2 = Long.toString(l1); System.out.println("Result = " + s1); System.out.println("Result = " + s2); } }
Enter Any Value = 32565432
Result = 32565432
Result = 32565432