Convert String to Long in Java

3 Java examples to convert String to long value using Long.parseLong(String), Long.valueOf(String) and new Long(String) constructor.

String number = "2018";			//String
		
long value1 = Long.parseLong( number );	//long - method 1

long value2 = Long.valueOf( number );	//long - method 2

long value3 = new Long( number );	//long - method 3

1. Long.valueOf(String) static method

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

1.1. String to long conversion example

In given example, we will convert one positive number and one negative number to long value.

// Positive long

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

// Negative long

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

System.out.println(value1);			//12001
System.out.println(value2);			//-22002

2. Long.parseLong(String) static method

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

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

2.1. String to long conversion example

Again, we will convert one positive number and one negative number to long value using Long.parseLong(String) static method.

// Positive long

String positiveNumber = "+1000";
long value1 = Long.parseLong(positiveNumber);

// Negative long

String negativeNumber = "-2000";
long value2 = Long.parseLong(negativeNumber);

System.out.println(value1);			//1000
System.out.println(value2);			//-2000

3. new Long(String) Constructor

Another useful way is to utilize Long class constructor to create new long object. Using unboxing feature, concert long object to long primitive.

// Positive long

String positiveNumber = "+1000";
long value1 = new Long(positiveNumber);

// Negative long

String negativeNumber = "-2000";
long value2 = new Long(negativeNumber);

System.out.println(value1);			//1000
System.out.println(value2);			//-2000

4. NumberFormatException Error

If the String argument does not have only decimal characters (except first character which can be plus or minus sign), you 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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.