在Java中将参考值相同的两个字符进行比较 [英] Compare reference two character with same value in Java
问题描述
我们知道Java中的运行时常量池存储以下文字: 1)整数2)长整数3)浮点数4)双精度5)字符串.因此,布尔文字和字符文字存储在哪里?
We know that the Runtime Constant Pool in Java stores the following literals: 1) Integer 2) Long 3) Float 4) Double 5) String . Hence, where are the Boolean Literals and Character Literals stored??
示例:
Character c1 = 'a';
Character c2 = 'a';
System.out.println(c1==c2); //prints true
由于==
比较引用,所以c1和c2共享相同的引用吗? (=请注意,这些文字的创建方式使其成为各自的原始包装器类的对象.
Since ==
compares references, does c1 and c2 share the same reference? (=Note that these literals are created such that they are the objects of the respective Primitive Wrapper Classes.
推荐答案
我们知道Java中的运行时常量池存储以下文字:1)整数2)Long 3)浮点数4)Double 5)String.
We know that the Runtime Constant Pool in Java stores the following literals: 1) Integer 2) Long 3) Float 4) Double 5) String .
实际上,这不是很正确.正确的说法是,在实践中对于每种类型的Boolean
,Byte
,Short
,Character
,Integer
和
Actually, that is not quite correct. A correct statement is that in practice there is a separate runtime pool for autoboxed values for each of the types Boolean
, Byte
, Short
, Character
, Integer
and Long
.
这些池不适用于 literals ...,因为包装类型的文字不存在 1 .这些类型的Java文字是基本类型的值. (实际上,根据JLS,类型byte
或short
...根本没有明显的文字.)
These pools are NOT for literals ... because literals of the wrapper types do not exist1. The Java literals for these types are values of primitive types. (And in fact, there are no distinct literals at all for types byte
or short
... according to the JLS.)
尽管valueOf
方法的javadocs似乎暗示了什么,但Float
和Double
没有自动装箱池. JLS不需要为Float
和Double
进行自动包装缓存,并且库也没有实现(至少在Java 6、7、8中).
There are NOT autoboxing pools for Float
and Double
, despite what the javadocs for the respective valueOf
methods seem to imply. The JLS does not require autobox caching for Float
and Double
, and the libraries do not implement it (at least in Java 6, 7, 8).
要注意的另一件事是,并非自动装箱生成的所有值都将在相应的池中.例如,(默认情况下)仅Integer
缓存-128到+127的值.
The other thing to note is that not all values produced by autoboxing will be in the respective pools. For instance, (by default) only Integer
caches the values for -128 through +127.
因此,布尔文字和字符文字存储在哪里?
Hence, where are the Boolean Literals and Character Literals stored?
答案是从上面得出的:
- 没有类型为
Boolean
和Character
的文字.
通过自动装箱创建的 -
Boolean
和Character
的实例可能来自各自的自动装箱池.
- There are no literals of type
Boolean
andCharacter
. - Instances of
Boolean
andCharacter
created by autoboxing may come out of the respective autoboxing pools.
以您的示例为例:
由于==比较引用,c1和c2是否共享相同的引用?
Since == compares references, does c1 and c2 share the same reference?
那是正确的.它说明了前面的观点. (通过JLS)需要Character
的自动装箱池,以便通过'\u007f'
缓存'\u0000'
.
That is correct. And it illustrates the previous point. The autoboxing pool for Character
is required (by the JLS) to cache '\u0000'
through '\u007f'
.
请注意,我省略了String
. String
池是不同的,因为它(主要是)用于字符串文字.另一方面,没有为字符串值定义自动装箱...因为字符串值不必与原始方法和包装方法二分法相抗衡.
Note that I left out String
. The String
pool is different because it is (primarily) for string literals. On the other hand, autoboxing is not defined for string values ... because string values don't have to contend with the primitive versus wrapper dichotomy.
1-对不起,我很讨厌,但是如果您想真正理解这些内容并与他人进行连贯的讨论,则必须使用正确的Java专用术语.严格来说,文字是>> sourcecode<<<一个程序.在运行时,相应的值是一个值.
这篇关于在Java中将参考值相同的两个字符进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!