jueves, 28 de noviembre de 2013

Cast from Object to Integer


Problem:

I need to cast from Object to Int.

Solution:

If you're sure that this object is an Integer :
int i = (Integer) object;
Or, starting from Java 7, you can equivalently write:
int i = (int) object;
Beware, it can throw a ClassCastException if your object isn't an Integer and aNullPointerException if your object is null.