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 −

示例

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);
   }
}

输出

String: 5
Converted to Long: 5

相关文章