我如何在java中将对象转换为int型?


当前回答

我在处理来自GSON的数据时使用一行程序:

int i = object != null ? Double.valueOf(object.toString()).intValue() : 0;

其他回答

so divide1=me.getValue()/2;

int divide1 = (Integer) me.getValue()/2;

假设对象是一个Integer对象,那么你可以这样做:

int i = ((Integer) obj).intValue();

如果对象不是Integer对象,则必须检测类型并根据其类型进行转换。

不可能做到。int不是一个对象,它是一个基本类型。你可以将它转换为Integer,然后获取int。

 Integer i = (Integer) o; // throws ClassCastException if o.getClass() != Integer.class

 int num = i; //Java 1.5 or higher

如果你想把字符串对象转换成整数… 你可以简单地传递为:

 int id = Integer.valueOf((String) object_name);

希望这对你有帮助:-)

对象变量;hastaId

Object hastaId = session.getAttribute("hastaID");

例如,将一个对象转换为int类型,即hasaid

int hastaID=Integer.parseInt(String.valueOf(hastaId));