Convert Object to Integer in java


I found this very tricky. I  first converted Object to String then used java’s Integer.parseInt(String s) method to change String to Integer. Then I added int value to the converted value, wihch verifies the object has been successfully converted to int.

public class  ObjectToInt {
public static void main(String args[]) {
Object object = "123";

		int value = 0 ; //int value = zero

/** Type Cast Object to String */
String tempString = (String) object;

/** use Integer.parseInt(String s) method to change to   int value */

		value = Integer.parseInt((String)object);

		//Finally Check the value

Leave a Reply

Your email address will not be published. Required fields are marked *