java.lang.Long parseUnsignedLong(String s))

Description

The Long.parseUnsignedLong(String s) java method Compares two long values numerically. Parses the string argument as an unsigned decimal long. The characters in the string must all be decimal digits, except that the first character may be an an ASCII plus sign ‘+’ (‘u002B’). The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseUnsignedLong(java.lang.String, int) method.

Make a note that the parseUnsignedLong(String s) method of Long class is static thus it should be accessed statically which means the we would be calling this method in this format:

Long.parseUnsignedLong(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java parseUnsignedLong method non statically.

Notes:

  • This method will throw NumberFormatException, if the string does not contain a parsable unsigned integer.

Method Syntax

public static long parseUnsignedLong(String s) throws NumberFormatException

Method Argument

Data Type Parameter Description
String s a String containing the unsigned long representation to be parsed

Method Returns

The parseUnsignedLong(String s) method of Long class returns the unsigned long value represented by the decimal string argument.

Compatibility

Requires Java 1.8 and up

Java Long parseUnsignedLong(String s) Example

Below is a simple java example on the usage of parseUnsignedLong(String s) method of Long class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * parseUnsignedLong(String s) method of Long class
 */

public class LongParseUnsignedLongExample {

	public static void main(String[] args) {
		
		// Ask for user input
		System.out.print("Enter a value:");
		
		// declare a scanner object to read the user input
		Scanner s = new Scanner(System.in);
		
		// assign the input to a variable
		String value = s.nextLine();
		
		
		// get the parseUnsignedLong() method result
		Long result = Long.parseUnsignedLong(value);
		
		// print the result
		System.out.println("Result:"+result);
		
		// close the scanner object
		s.close();
		
		/*
		 * This method will yield the same result as that of the parseUnsignedLong(value,radix)
		 * if the radix is 10  
		 */
		
		
	}

}

Sample Output

Below is the sample output when you run the above example.

Java Long parseUnsignedLong(String s) example output