Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Convert String to Long

  • Difficulty Level : Expert
  • Last Updated : 09 Oct, 2021

Given a String, the task is to convert the given String to Long in Java.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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.



Approaches

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

1. Using Long.parseLong() method

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); 
    
}
Output
String - 999999999999
Long - 999999999999

2. Using Long.valueOf() method

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); 
    
}
Output
String - 999999999999
Long - 999999999999

3. Using Constructor of Long class

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :