Java toString convert to long

www.lautt‮moc.iru‬
Java toString convert to long

In Java, you can use the Long.parseLong() method to convert a string representation of a long integer value into a long primitive type.

Here's an example of how you can use the Long.parseLong() method to convert a string to a long value:

String str = "12345";
long l = Long.parseLong(str);

In this example, the str variable is a string that represents the long integer value 12345. The Long.parseLong() method is called on the str variable and the resulting long value is stored in the l variable.

The Long.parseLong() method takes a string as an argument and returns a long value. If the string is not a valid long integer representation, the method will throw a NumberFormatException.

You can also use the Long.valueOf() method to convert a string to a Long object, which is the wrapper class for the long primitive type. The Long.valueOf() method works in a similar way to the Long.parseLong() method, but it returns a Long object instead of a long value.

Here's an example of how you can use the Long.valueOf() method to convert a string to a Long object:

String str = "12345";
Long l = Long.valueOf(str);

In this example, the str variable is a string that represents the long integer value 12345. The Long.valueOf() method is called on the str variable and the resulting Long object is stored in the l variable.

It's important to note that both the Long.parseLong() and Long.valueOf() methods can throw a NumberFormatException if the string is not a valid long integer representation. You should catch this exception or use try-with-resources to handle it appropriately.

Created Time:2017-10-17 20:18:54  Author:lautturi