请问各位网友,以下两句中,currentPage得到的值有何不同?
currentPage = Integer.parseInt(request.getParameter("page"));
currentPage = ((Integer) (request.getAttribute("currentPage"))).intValue();
currentPage = Integer.parseInt(request.getParameter("page"));
currentPage = ((Integer) (request.getAttribute("currentPage"))).intValue();
request.getAttribute("currentPage")
一样吗?
这行代码表示把一个String解析成一个int,因为request.getParameter("page")取出来的是StringcurrentPage = ((Integer) (request.getAttribute("currentPage"))).intValue();
这行代码表示把一个Object先类型转换成Integer,然后再取出int值,因为request.getAttribute("currentPage")取出来的是Object,但是一定要保证这个Object是Integer,否则可能会出现类型转换异常,好像是ClassCastException
转换过程都是Object->Integer->int