Integer.valueOf(String) 之困惑

本文详细解析了Java中Integer.valueOf()方法在处理不同字符串输入时的行为差异,包括缓存机制、对象创建过程及比较运算的实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在最前:

此文只适应于jdk7或以上版本,因为jdk6与jdk7的Integer具体实现有差别,详情可查看下源代码.


问题来源:

StackOverflow 上发起了一个问题是这么问的:“ 我被下面的代码搞晕了,为什么它们会返回不同的值?”
System.out.println(Integer.valueOf(“127”)==Integer.valueOf(“127”));
System.out.println(Integer.valueOf(“128”)==Integer.valueOf(“128”));
System.out.println(Integer.parseInt(“128”)==Integer.valueOf(“128”));

输出是:
1.true
2.false
3.true

问题:

1.为什么第一个判断返回了true而第二个判断返回了false?127和128有什么我不知道的区别吗?
2.为什么第三个判断返回了true?

回答#1:

Integer.valueOf(String)确有一个不同寻常的行为。

valueOf会返回一个Integer(整型)对象,当被处理的字符串在-128和127(包含边界)之间时,返回的对象是预先缓存的。这就是为什么第一行的调用会返回true-127这个整型对象是被缓存的(所以两次valueOf返回的是同一个对象)——第二行的调用返回false是因为128没有被缓存,所以每次调用,都会生成一个新的整型对象,因此两个128整型对象是不同的对象。

重要的是你要知道在上面的比较中,你实际进行比较的是integer.valueOf返回的对象引用,所以当你比较缓存外的整型对象时,相等的判断不会返回true,就算你传个valueOf的值是相等的也没用。(就像第二行中Integer.valueOf(128)==Integer.valueOf(128))。想让这个判断返回true,你需要使用equals()方法。

parseInt()返回的不是整型对象,而是一个int型基础元素。这就是为什么最后一个判断会返回true,第三行的判断中,在判断相等时,实际比较的是128 == 128,所以它必然是相等的。

再来说说第三种比较中的一点区别,使得它的结果与第二种比较不一样了:

一个unboxing conversion(一种比较时的转换,把对对象的引用转换为其对应的原子类型)在第三行的比较中发生了。因为比较操作符使用了==同时等号的两边存在一个int型和一个Integer对象的引用。这样的话,等号右边返回的Integer对象被进一步转换成了int数值,才与左边进行相等判断。

所以在转换完成后,你实际比较的是两个原子整型数值。这种转换正是你在比较两个原子类型时所期待看到的那样,所以你最终比较了128等于128。

回答#2:

Integer类有一个静态缓存,存储了256个特殊的Integer对象——每个对象分别对应`-128 和127之间的一个值。有了这个概念,就可以知道上面三行代码之间的区别。

new Integer(123);

显示创建了一个新的Integer对象。

Integer.parseInt(“123”);

解析完字符串后返回一个int值。

Integer.valueOf(“123”);

这种情况比其他的要更复杂一些。首先进行了字符串解析,然后如果解析的值位于-128和127之间,就会从静态缓存中返回对象。如果超出了这个范围,就会调用Integer()方法并将解析的值作为参数传入,得到一个新的对象。

现在,让我们看一下问题中的3个表达式。

Integer.valueOf(“127”)==Integer.valueOf(“127”);
上面的表达式返回true,因为Integer的值从静态缓存中取了2次,表达式返回了对象与自己比较的结果。因为只有一个Integer对象,所以返回结果为true。

Integer.valueOf(“128”)==Integer.valueOf(“128”);
上面的表达式返回false,因为128没有存在静态缓冲区。所以每次在判断相等时等式两边都会创建新的Integer对象。由于两个Integer对象不同,所以==只有等式两边代表同一个对象时才会返回true。因此,上面的等式返回false。

Integer.parseInt(“128”)==Integer.valueOf(“128”);
上面的表达式比较的是左边的原始int值128与右边新创建的Integer对象。但是因为int和Integer之间比较是没有意义的,所以Java在进行比较前会将Integer自动拆箱,所以最后进行的是int和int值之间的比较。由于128和自己相等,所以返回true。

Objectives of this Assignment 1. Declare arrays of different types dynamically. 2. Iterate through arrays, processing all of the elements. 3. Write methods with arrays as parameters and return values In this assignment you will create your own class and write the methods in it. The class you write is called Main and it has four methods that build arrays and four methods that process arrays. All methods should be public and static. Create a project called P7_3 and a class named Main in a file calledMain.java, then follow the instructions below exactly: 1. Write a method named createStrings that takes a String as an input parameter, and returns an array of string with 4 elements. The first element is the original string passed in, the second element is the same string converted to uppercase, the third element is the same string converted to lowercase, and the fourth element is the same string with the first character removed. 2. Write a method called charFrequency that takes an array of strings and a single character as parameters. The method should iterate the array, counting instances of the character passed in. For example, if the array is ["Hello", "There"] and we are asked to count the character 'e', the return value will be three. If the character is not found in any of the elements in the array, the return value should be zero. 3.Add a main method with the usual signature that instantiates the Main class and tests its methods as follow: public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); char ch = in.next().charAt(0); // Create arrays String[] stringArray = createStrings(str); // Test processing System.out.println(charFrequency(stringArray, ch)); in.close(); } Input Specification: There are two lines in the input. The first line is the string to be converted. The second line contains one character to be counted. Output Specification: For each case, out
03-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值