Open In App

How to Convert a String to an Long in Java ?

In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class.

Example of String to Long Conversion

Input: " 9876543210"
Output: 9876543210

Program Convert String to Long in Java

The Most basic method we can come up with to convert String to Long in Java is using Constructor:




// Java Program to Convert String to Long
// Using Constructor of Long class
import java.io.*;
import java.util.*;
  
// Driver Class
class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input string
        String str = "9876543210";
        System.out.println("String : " + str);
  
        // Converting above string to long
        // using Long(String s) constructor
        Long num = new Long(str);
  
        // Printing the above long value
        System.out.println("Long : " + num);
    }
}

Output
String : 9876543210
Long : 9876543210

Explanation of the above program:

Article Tags :