Java - Long parseLong() method



Description

The Java Long parseLong(String s, int radix) parseLong(String s, int radix) method parses the string argument s as a signed long in the radix specified by the second argument radix.Some examples can be seen here −

parseLong("0", 10) returns 0L
parseLong("11", 10) returns 111L
parseLong("-0", 10) returns 0L
parseLong("-BB", 16) returns -187L
parseLong("1010110", 2) returns 86L
parseLong("ADMIN", 27) returns 5586836L
parseLong("99", 8) throws a NumberFormatException

Declaration

Following is the declaration for java.lang.Long.parseLong() method

public static long parseLong(String s, int radix) throws NumberFormatException

Parameters

  • s − This is a String containing the long representation to be parsed.

  • radix − This is the radix to be used while parsing s.

Return Value

This method returns the long represented by the string argument in the specified radix.

Exception

NumberFormatException − if the string does not contain a parsable long.

Example 1

The following example shows the usage of Long parseLong() method to parse a Long object from a string containing decimal number. We've created a String variable and assign it a string containing decimal number. Then using parseLong method, we're obtaining the Long object and printing it.

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
    
      String str = "50";
      /* returns an Long object holding the int value represented
         by string str */
      System.out.println("Number = " + Long.parseLong(str, 10)); 
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 50

Example 2

The following example shows the usage of Long parseLong() method to get a Long object from a string containing a octal number. We've created a String variable and assign it a string containing an octal number. Then using parseLong method, we're obtaining the Long object and printing it.

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
    
      String str = "50";
      /* returns an Long object holding the octal int value represented
         by string str */
      System.out.println("Number = " + Long.parseLong(str,8)); 
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 40

Example 3

The following example shows the usage of Long parseLong() method to get a Long object from a string containing a hexadecimal number. We've created a String variable and assign it a string containing an hexadecimal number. Then using parseLong method, we're obtaining an Long object and printed.

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
    
      String str = "50";
      /* returns an Long object holding the hexadecimal int value represented
         by string str */
      System.out.println("Number = " + Long.parseLong(str,16)); 
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 80
java_lang_long.htm
Advertisements