New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs

Java Long parseLong (String s) Method

Java parseLong(String s) method is the part of the Long class of the java.lang package. This method is used to parse the string value as a signed decimal long value.

It must be noted that the characters passed in the string must be all decimal except for the first character which is used to define the sign of the long value. It may be an ASCII minus '-' sign to depict a negative value or an ASCII plus '+' sign to depict a positive value. Also, note that neither the character L ('\u004C') nor l ('\u006C') is permitted to appear at the end of the string as a type indicator, as would be permitted in Java programming language source code.

Syntax:

public static long parseLong(String s) throws NumberFormatException

Parameters:

The parameter passed is the string value whose signed decimal Long object is to be returned.

Returns:

It returns the signed decimal Long object of the string value passed as a parameter.

Example 1:

Here, two strings are passed and are returned as their respective signed decimal Long objects.

import java.lang.Long;

public class StudyTonight 
{  
	public static void main(String[] args) 
	{  
		String s1 = "23"; 
		String s2 = "-23";
		long n1 = Long.parseLong(s1);  //converts the passed string as signed positive long
		long n2 = Long.parseLong(s2);  //converts the passed string as signed negative long

		System.out.println("Equivalent Long object is : " + n1);
		System.out.println("Equivalent Long object is : " + n2);
	}  
}  


Equivalent Long object is : 23
Equivalent Long object is : -23

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.Long;
import java.util.Scanner;

public class StudyTonight 
{  
	public static void main(String[] args) 
	{
		try
		{
			System.out.println("Enter Value");
			Scanner sc = new Scanner(System.in);
			String s = sc.nextLine();
			long n = Long.parseLong(s);  //converts the passed string as signed long
			System.out.println("Equivalent Long object is : " + n);
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
}


Enter Value
866
Equivalent Long object is : 866
****************************************
Enter Value
-534
Equivalent Long object is : -534
****************************************
Enter Value
0x545
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.