Q:

Conversion from String to Long in Java

belongs to collection: Java Conversion Programs

0

Given a string and we have to convert it into a long.

Java conversion from String to Long

To convert a String to Long, we can use the following methods of Long class (see the syntax given below...)

Syntax:

    Long Long.valueOf(String).longValue();
    OR
    Long Long.parseLong(String);

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Java code to convert a String to Long

//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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Conversion from String to Float in Java... >>
<< Conversion from String to Double in Java...