The Long.parseLong() static method parses the string argument as a signed decimal long and returns a long value.
Syntaxpublic static long parseLong(String s) throws NumberFormatException
The parameter s will be converted to a primitive long value. Note that the method will throw a NumberFormatException if the parameter is not a valid long.
ExampleString numberAsString = "1234"; long number = Long.parseLong(numberAsString); System.out.println("The number is: " + number);When you run the code above, the output will show this:
The number is: 1234This is the most common and popular solution when you need to convert a String to long. Note that the resulting value is not an instance of the Long class but just a plain primitive long value.
The Long.valueOf() static method will return an Long object holding the value of the specified String.
Syntaxpublic static Long valueOf(String s) throws NumberFormatException
Note that the method will throw a NumberFormatException if the parameter is not a valid long.
String numberAsString = "1234"; long number = Long.valueOf(numberAsString); System.out.println("The number is: " + number);This will output:
The number is: 1234This is the most common method when you wish to convert a String to Long. Note that the resulting value is an instance of the Long class and not a primitive long value.
Another alternative method is to create an instance of Long class and then invoke it's longValue() method.
Example
String numberAsString = "1234"; Long longObject = new Long(numberAsString); long number = longObject.longValue();
We can shorten to:
String numberAsString = "1234"; long number = new Long(numberAsString).longValue();or just:
long number = new Long("1234").longValue();
String numberAsString = "1234"; DecimalFormat decimalFormat = new DecimalFormat("#"); try { long number = decimalFormat.parse(numberAsString).longValue(); System.out.println("The number is: " + number); } catch (ParseException e) { System.out.println(numberAsString + " is not a valid number."); }Will output
The number is: 1234Note that the parse() method will throw a ParseException when there are problems encountered with the String.
All of the examples above uses the base (radix) 10. There are cases when we wish to convert a Java String to Long but using another base. Both Long.parseLong() and Long.valueOf() can receive a custom radix to be used in the conversion
Examples
String numberAsString = "11111111"; long number1 = Long.valueOf(numberAsString, 2); long number2 = Long.parseLong(numberAsString, 2); System.out.println(number1); System.out.println(number2);Will output:
255 255
String numberAsString = "377"; long number1 = Long.valueOf(numberAsString, 8); long number2 = Long.parseLong(numberAsString, 8); System.out.println(number1); System.out.println(number2);Will output:
255 255
String numberAsString = "ff"; long number1 = Long.valueOf(numberAsString, 16); long number2 = Long.parseLong(numberAsString, 16); System.out.println(number1); System.out.println(number2);Will output:
255 255