Skip to Content

Convert String to long Example

  1. /*
  2.   Convert String to long Example
  3.   This example shows how a String object can be converted into long primitive type
  4.   using parseLong method of Long class.
  5. */
  6.  
  7. public class StringToPrimitiveLongExample {
  8.  
  9.   public static void main(String[] args) {
  10.     //declare String object
  11.     String str = new String("10");
  12.    
  13.     /*
  14.     use parseLong method of Long class to convert String into long primitive
  15.     data type. This is a static method.
  16.     Please note that this method can throw a NumberFormatException if the string
  17.     is not parsable to long.
  18.     */
  19.     long l = Long.parseLong(str);
  20.     System.out.println(l);
  21.   }
  22. }
  23.  
  24. /*
  25. Output the program would be
  26. 10
  27. */