Convert String to Long in Java


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

276 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements