logo image

How to convert String into long in Java?



There are two ways you can convert a String into long in Java

  1. Using Long.parseLong(String input) - This returns primitive int
String input = "12345678";
long output = Long.parseLong(input);
System.out.println(output);
  1. Using Long.valueOf(String input) - This returns instance of Long type
String input = "12345678";
Long output = Long.valueOf(input);
System.out.println(output);

Note: Code will through NumberFormatException if you do not pass a valid long String. Ex. if you pass 123.23 that causes NumberFormatException.