Java.lang.Long.parseLong() Method

Advertisements


Description

The java.lang.Long.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

The following example shows the usage of java.lang.Long.parseLong() method.

package com.tutorialspoint;

import java.lang.*;

public class LongDemo {

   public static void main(String[] args) {

     // parses the string with specified radix
     long a = Long.parseLong("0", 10);
     System.out.println(a);
 
     long b = Long.parseLong("111", 10);
     System.out.println(b);
  
     long c = Long.parseLong("-0", 10);
     System.out.println(c);
   
     long d = Long.parseLong("-BB", 16);
     System.out.println(d);
   
     long e = Long.parseLong("1010110", 2);
     System.out.println(e);
   
     long f = Long.parseLong("2147483647", 10);
     System.out.println(f);

     long g = Long.parseLong("-2147483648", 10);
     System.out.println(g);
   
     long h = Long.parseLong("ADMIN", 27);
     System.out.println(h);
   }
}  

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

 0
111
0
-187
86
2147483647
-2147483648
5586836



Advertisements
Advertisements