Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Convert String to Integer example

  1. /*
  2.  * Convert String to Integer example. Program to convert Java
  3.  * String object to Integer object.
  4.  */
  5.  
  6. public class StringToInteger {
  7.  
  8.  public static void main(String args[]){
  9.  
  10.   //new String object
  11.   String str = new String("47");
  12.  
  13.   //Use Integer(String) constructor
  14.   Integer iObj1 = new Integer(str);
  15.  
  16.   //OR Use static valueOf(String) method of Integer class
  17.   Integer iObj2 = Integer.valueOf(str); //throws NumberFormatException
  18.    
  19.   System.out.println("String converted to Integer: " + iObj1);
  20.  }
  21.  
  22. }
Your rating: None Average: 1 (1 vote)
Share this

Post new comment

11 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.