首页 技术 正文
技术 2022年11月16日
0 收藏 663 点赞 4,632 浏览 4205 个字

一、Integer类的使用方法

Interger:整数类型

1、属性。

static int MAX_VALUE:返回最大的整型数;

static int MIN_VALUE:返回最小的整型数;

static Class TYPE :返回当前类型。

例子:

System.out.println(“Integer.MAX_VALUE: ” + Integer.MAX_VALUE );

结果为:Integer.MAX_VALUE: 2147483647

2、构造函数。

Integer(int value) :通过一个int的类型构造对象;

Integer(String s) :通过一个String的类型构造对象;

例子:

Integer i = new Integer(“1234”);

生成了一个值为1234的Integer对象。

3、方法。

说明:

1. 所有方法均为public;

2. 书写格式:[修饰符] <返回类型> <方法名([参数列表])>

如:

static int parseInt(String s) 表示:此方法(parseInt)为类方法(static),返回类型为(int),方法所需参数为String类型。

1. byteValue():取得用byte类型表示的整数;

2. int compareTo(Integer anotherInteger) :比较两个整数。相等时返回0;小于时返回负数;大于时返回正数。

例子:

Integer i = new Integer(1234);

System.out.println(“i.compareTo: ” + i.compareTo(new Integer(123)) );

结果为:i.compareTo: 1

 

3. int compareTo(Object o) :将该整数与其他类进行比较。如果o也为Integer类,进行方法2 的操作;否则,抛出ClassCastException异常。

4. static Integer decode(String nm) :将字符串转换为整数。

5. double doubleValue() :取得该整数的双精度表示。

6. boolean equals(Object obj) :比较两个对象。

7. float floatValue() :取得该整数的浮点数表示。

8. static Integer getInteger(String nm) :根据指定名确定系统特征值。

9. static Integer getInteger(String nm, int val) :上面的重载。

10. static Integer getInteger(String nm, Integer val) :上面的重载。

11. int hashCode() :返回该整数类型的哈希表码。

12. int intValue() : 返回该整型数所表示的整数。

13. long longValue() :返回该整型数所表示的长整数。

14. static int parseInt(String s) :将字符串转换成整数。s必须是时进制数组成,否则抛出NumberFormatException异常。

15. static int parseInt(String s, int radix) :以radix为基数radix返回s的十进制数。所谓的基数,就是“几进制”。

例子:

String s1 = new String(“1010”);

System.out.println(“Integer.parseInt(String s, int radix): ” + Integer.parseInt(s1,2) );

结果为:Integer.parseInt(String s, int radix): 10

16. short shortValue() :返回该整型数所表示的短整数。

17. static String toBinaryString(int i) :将整数转为二进制数的字符串。

18. static String toHexString(int i) :将整数转为十六进制数的字符串。

19. static String toOctalString(int i) :将整数转为八进制数的字符串。

20. String toString() :将该整数类型转换为字符串。

21. static String toString(int i) :将该整数类型转换为字符串。不同的是,此为类方法。

22. static String toString(int i, int radix) :将整数i以基数radix的形式转换成字符串。

例子:

int i1 = 54321;

System.out.println(“Integer.toString(int i, int radix): ” + Integer.toString(i1,16) );

结果为:Integer.toString(int i, int radix): d431

 

23. static Integer valueOf(String s) :将字符串转换成整数类型。

24. static Integer valueOf(String s, int radix) :将字符串以基数radix的要求转换成整数类型。

二、javaWeb中request的setAttribute()方法的使用

在两个JSP代码片中有这样两端程序:

JSP1代码

[java] view
plain
 copy

1. String [] test=new String[2];

2. test[0]=”1″;

3. test[1]=”2″;

4. request.setAttribute(“test”,test) ;

5. response.sendRedirect(“jsp2.jsp”);

JSP2代码

//在两个JSP代码片中有这样两端程序://JSP1代码
//[java] view plain copy
1.String [] test=new String[2];  
2.test[0]="1";  
3.test[1]="2";  
4.request.setAttribute("test",test) ;  
5.response.sendRedirect("jsp2.jsp");  //JSP2代码
//[java] view plain copy
1.</pre><pre name="code" class="java">String test[]=(String[])request.getAttribute("test");  
2.out.print(test);  

但是,问题来了:为什么JSP2
中test获取不到呢?

其实那就要从request的生命周期或者是说作用范围说起了,setAttribute()用来在同一个request周期中保存变量使用。

比如servlet调用后,引出JSP页面,这是一个request周期。如果在Jsp页面需要servlet中的一些
处理结构,就从request.getAttribute中获取。

而sendRedirect()方法是通过浏览器重定向的,所以第二个JSP页面中获得的request并非是前一个页面的request(两次请求生成了前后两个不同的
request对象了)。

而此时使用RequestDispatcher接口的forward()方法则能够得到request中的对象了,这是因为后者并不是使用客户端浏览器进行重定向的,从函数的名字就可以看出,RequestDispatcher.forward()就是从服务器端进行任务转发。

sendRedirect()是请求从定向,和超连接是一个意思,比如你在A页面中写一个request.setAtribute,sendRedirect到B页面,就是说服务器从A页面中给你一个response,然后你的浏览器再去request到B页面,由于有两次request和response,是不能在B页面取到request.setAtribute里的值,能从地址栏看到url的改变。

request.getRequestDispatcher().forward(request,response)是请求分发器,比如你在A页面中写一个request.setAtribute,request.getRequestDispatcher().forward(request,response)到B页面,那就是说服务器给你的response是B页面的,并且只有一次request和response,所以是能在B页面取到request.setAtribute里的值,地址栏的url仍然是A页面的。

所以通常情况下,setAttribute()方法都和RequestDispatcher.forward()都在一起使用,具体用法示例:

[java] view
plain
 copy

1. List list = new CommonsDao().getAllCommons();

2. request.setAttribute(“CommonsList”, list);

3. request.getRequestDispatcher(“View.jsp”).forward(request, response);

顺便谈一谈 request.getAttribute()与request.setAttribute()

request.getAttribute(“nameOfObj”),可得到JSP页面一表单中控件的Value。

其实表单控件中的Object的
name与value是存放在一个哈希表中的,所以在这里给出Object的name会到哈希表中找出对应它的value。

而不同页面间传值使用request.setAttribute(position, nameOfObj)时,只会从a.jsp到b.jsp一次传递,之后这个request
就会失去它的作用范围,再传就要再设一个 request.setAttribute()。而使用session.setAttribute()会在一个过程中始终保有这个值。

P.S.:JavaScript与JSP中不能相互传值,因为JavaScript运行在客户端,而JSP运行在服务器端。若想使它们之间可以相互传递参数,可以在JSP中设置一个hidden控件,用它的value结合上面所说的用法来传递所需的数值。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,429
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726