Convert String to long in Java

Learn to convert a String to Long type in Java using Long.parseLong(String), Long.valueOf(String) methods and new Long(String) constructor.

String number = "2018"; //String

long value1 = Long.parseLong( number, 10 ); 

long value2 = Long.valueOf( number ); 

long value3 = new Long( number ); 

1. Using Long.valueOf(String)

The Long.valueOf() method parses the input string to a signed decimal long type. The characters in the string must all be decimal digits, except that the first character may be a minus (-) sign for negative numbers and a plus(+) sign for positive numbers.

The result long value is exactly the same as the string argument in base 10. In the following example, we convert one positive and one negative number to a long value.

String positiveNumber = "+12001";
long value1 = Long.valueOf(positiveNumber);   //12001L

String negativeNumber = "-22002";
long value2 = Long.valueOf(negativeNumber);    //-22002L

If the string cannot be parsed as a long, it throws NumberFormatException.

Assertions.assertThrows(NumberFormatException.class, () -> {
  Long.valueOf("alexa");
});

2. Using Long.parseLong(String)

The rules for Long.parseLong(String) method are similar to Long.valueOf(String) method as well.

  • It parses the String argument as a signed decimal long type value.
  • The characters in the string must all be decimal digits, except that the first character may be a minus (-) sign for negative numbers and a plus(+) sign for positive numbers.
  • The result long value is exactly the same as the string argument in base 10.

Again, we will convert one positive number and one negative number to long value using parseLong() API.

String positiveNumber = "+12001";
long value1 = Long.parseLong(positiveNumber);   //12001L

String negativeNumber = "-22002";
long value2 = Long.parseLong(negativeNumber);    //-22002L

If the input String is in another base then we can pass the base as second input to the method.

String numberInHex = "-FF";

long value = Long.parseLong(numberInHex);   //-255L

3. Using new Long(String) Constructor

Another useful way is to utilize Long class constructor to create new long object. This method has been deprecated since Java 9, and recommended to use parseLong() API as discussed above.

long value = new Long("100");   //100L

4. NumberFormatException

Using any of the given approaches, if the input String does not have only the decimal characters (except the first character, which can be plus or minus sign), we will get NumberFormatException error in runtime.

String number = "12001xyz";

long value = Long.parseLong(number);

//Error

Exception in thread "main" java.lang.NumberFormatException: For input string: "12001xyz"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.<init>(Long.java:965)
at com.howtodoinjava.StringExample.main(StringExample.java:9)

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode