Java Program to Convert String to Long - GeeksforGeeks

09 Oct, 2021
Given a String, the task is to convert the given String to Long in Java.
Examples:
Input : String = "20"
Output: 20

Input : String = "999999999999"
Output: 999999999999
Strings: Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well. 
Long: The long data type is a 64-bit two’s complement integer. Its value range lies between (-263) to (263 -1)(inclusive). Its default value is 0. The long data type is used when you need a range of values more than those provided by int.
There are numerous approaches to convert a String to a Long data type in Java. A few of them are listed below.
Using Long.parseLong() method
Using Long.valueOf() method
Using Constructor of Long class
Long.parseLong() method is a method in which all the characters in the String must be digits except the first character, which can be a digit or a minus ‘-‘. 
Syntax:
Long varLong=Long.parseLong(str);
Java
// Java program to convert String to Long // using parseLong()     public class GFG {         // main method     public static void main(String args[])     {             // create a String         String str = "999999999999";           System.out.println("String - " + str);             // convert into Long         long varLong = Long.parseLong(str);             // print String as Long         System.out.println("Long - " + varLong);     } }
String - 999999999999
Long - 999999999999
Long.valueOf() method is a method that converts the String to a long value. Similar to parseLong(String) method, this method also allows minus ‘-‘ as a first character in the String.
Syntax:
long varLong = Long.valueOf(str);
Java
// Java program to convert String to Long// using valueOf()     public class GFG {         // main method     public static void main(String args[])     {             // create a String         String str = "999999999999";           System.out.println("String - " + str);            // convert into Long        long varLong = Long.valueOf(str);             // print String as Long        System.out.println("Long - " + varLong);     } }
String - 999999999999
Long - 999999999999
The long class has a constructor which allows the String argument and creates a new Long object representing the specified string in the equivalent long value. 
Java
import java.util.*;import java.io.*;  class GFG {    public static void main (String[] args) {               String str = "999999999";       System.out.println("String - " + str);                //Conversion using Long(String s) constructor       long num = new Long(str);                System.out.println("Long - " + num);    }}
Output
String - 999999999
Long - 999999999
nishkarshgandhi
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?