我知道堆的PermGen区域中String池的概念.所以,当我们做类似的事情
String firstString = "Stack";
String secondString = "Stack";
Run Code Online (Sandbox Code Playgroud)
两个引用firstString和secondString指向池中的同一个对象.但我尝试使用int类型的变量.
int firstInt = 5;
int secondInt = 5;
if(firstInt == secondInt) {
System.out.println("Both point to same allocated memory");
} else {
System.out.println("Both point to different allocated memory");
}
Run Code Online (Sandbox Code Playgroud)
结果是Both point to same object,当我尝试
Integer firstInteger = new Integer(2);
Integer secondInteger = new Integer(2);
if(firstInteger == secondInteger) {
System.out.println("Both point to same object");
} else {
System.out.println("Both point to different object");
}
Run Code Online (Sandbox Code Playgroud)
输出是 Both point to different object
我尝试了相同的char,结果是相似的.所以我的问题是我们有所有原始类型的池,比如int,char?当我们实际使用new ()与上述第二种情况相同的内容创建对象时,对象是克隆并存储在同一个池区域还是在池外?
fdr*_*ger 11
你的帖子中存在很多误解,甚至很难开始解释.得到一些体面的书.目前,一些可能对您有帮助的事实:
new,你无论如何都绕过了游泳池; 所以执行new String("ala")将始终创建一个新的String对象; 你无法改变语义new;Integer.valueOf),它们 - 在某种程度上 - 池实例(汇集所有可能的值是不可行或有益的Integers,Floats等等).