Suppose we want to create an Integer object. We can do this like any object using the new keyword, as follows:
Integer intObj = new Integer(7);
To convert a String to an Integer, use the java.lang.Integer.valueOf method, for example
String snum = "456";
Integer intObj = Integer.valueOf(snum);

To convert an Integer object to an int primitive, we can use the
java.lang.Integer.intValue method,
for example
int count = intObj.intValue();
To convert a String to an int primitive, we can perform the previous two statements in one step using the java.lang.Integer.parseInt method, for example
int count = Integer.parseInt(snum);
No comments:
Post a Comment