Convert String to Long in Java

Java 8Object Oriented ProgrammingProgramming

To convert String to Long, use the valueOf() method.

Let’s say the following is our string.

// string
String myStr = "5";
System.out.println("String: "+myStr);

To convert it to Long, we have used valueOf() −

Long longObj = Long.valueOf(myStr);

The following is the complete example to convert a String value to Long −

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      // string
      String myStr = "5";
      System.out.println("String: "+myStr);
      // long
      Long longObj = Long.valueOf(myStr);
      System.out.println("Converted to Long: "+longObj);
   }
}

Output

String: 5
Converted to Long: 5
raja
Published on 26-Dec-2018 11:07:52
Advertisements