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
Long.valueOf(String)
method parses thestring
argument as a signed decimallong
.- 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.
- 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.
Long.parseLong(String)
method parses thestring
argument as a signed decimallong
.- 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.
- 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
constructor to create new long object. Using unboxing feature, concert long object to long primitive.Long
class
// 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 !!