Conversion From String to Long in Java

You are currently viewing Conversion From String to Long in Java

Conversion from String to Long in Java

//Java code to convert String to Long
public class Main {
    public static void main(String args[]) {
        String str = "1234587878";

        //variable to store result
        long result = 0;

        //converting string to long
        //method 1
        result = Long.valueOf(str).longValue();
        System.out.println("result (value of str as long) = " + result);

        //method 2
        result = Long.parseLong(str);
        System.out.println("result (value of str as long) = " + result);
    }
}

Output

result (value of str as long) = 1234587878
result (value of str as long) = 1234587878

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply