<%@ page import="java.util.*"%>
<%@ page language="java" contentType="text/html;charset=gb2312"%>
[img]http://dl2.iteye.com/upload/attachment/0096/1262/94484d69-36dc-3535-bac7-40bbd90d8b04.jpg[/img]
既然application.getAttribute("num")返回类型是String,那么为什么把//mark处的代码改为num=Integer.parseInt(application.getAttribute("num");则会提示:The method parseInt(String) in the type Integer is not applicable for the arguments (Object)?
因为你往里面放的就是String 所以获取的时候就是java.lang.String 若是你往里面放Integer,则取出来就是java.lang.Integer,
所以application.getAttribute("num").getClass().getName()取出来是实际的数据类型
但是application.getAttribute("num")取出来的是Object,因为Object是所有的类的父类,你可以使用instanceof进行判断
第一,返回的是Object,需要强转
第二,String也属于object
第三,application.getAttribute("num").getClass().getName()里的
getname()方法返回的是一个字符串
1、application.getAttribute("num")返回的是Object类型的,不是String类型的。
2、Integer.parseInt(String)参数为String类型不是Object类型。
3、你的写法太复杂了,可以直接Integet num = (Integer)application.getAttribute("num")。
说有的getAttribute()方法返回的都是Object对象。需要强制类型转换。