by @codeexamples

Convert a string to long in Java

Home » java » Convert a string to long in Java

To convert a string to long, we can use the Long.valueOf() method in Java.

Here is an example:

String str = "1234854";

long number = Long.valueOf(str);

System.out.println(number);

Output:

1234854

You can also use new Long() constructor to convert a string to long.

String str = "1234854";

long number = new Long(str);

System.out.println(number);  // 1234854