Java - Long parseLong() method



Description

The Java Long parseLong(String s) method parses the string argument s as a signed decimal long.

Declaration

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

 public static long parseLong(String s) throws NumberFormatException 

Parameters

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

Return Value

This method returns the long value represented by the argument in decimal.

Exception

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

Getting a long value from a String Containing Positive Decimal Number Example

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 long value represented by string str */ System.out.println("Number = " + Long.parseLong(str)); } } 

Output

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

 Number = 50 

Getting a long value from a String Containing Negative Decimal Number Example

The following example shows the usage of Long parseLong() method to get a Long object from a string containing a negative decimal number. We've created a String variable and assign it a string containing a negative 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 long value represented by string str */ System.out.println("Number = " + Long.parseLong(str)); } } 

Output

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

 Number = -50 

Facing Exception while Getting a long value from a String Containing Invalid Number Example

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 trying to obtain an Long object and exception will be raised.

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

Output

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

 Exception in thread "main" java.lang.NumberFormatException: For input string: "0x3"	at java.lang.NumberFormatException.forInputString(Unknown Source)	at java.lang.Long.parseLong(Unknown Source)	at java.lang.Long.parseLong(Unknown Source)	at com.tutorialspoint.LongDemo.main(LongDemo.java:10) 
java_lang_long.htm
Advertisements