java 变量分配_Java中成员变量分配在哪个空间?

常量池的说明

常量池之前是放在方法区里面的,也就是在永久代里面的,从JDK7开始移到了堆里面。这一改变我们可以从oracle的release version的notes里的** Important RFEs Addressed in JDK 7 **看到。Area: HotSpot

Synopsis: In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

RFE: 6962931

String内存位置说明显式的String常量String a = "holten";

String b = "holten";第一句代码执行后就在常量池中创建了一个值为holten的String对象;

第二句执行时,因为常量池中存在holten所以就不再创建新的String对象了。

此时该字符串的引用在虚拟机栈里面。String对象String a = new String("holtenObj");

String b = new String("holtenObj");Class被加载时就在常量池中创建了一个值为holtenObj的String对象,第一句执行时会在堆里创建new String("holtenObj")对象;

第二句执行时,因为常量池中存在holtenObj所以就不再创建新的String对象了,直接在堆里创建new String("holtenObj")对象。

验证一下/**

* Created by holten.gao on 2016/8/16.

*/public class Main {    public static void main(String[] args){

String str1 = "高小天";

String str2 = "高小天";

System.out.println(str1==str2);//true

String str3 = new String("高大天");

String str4 = new String("高大天");

System.out.println(str3==str4);//false

}

}

返回结果:truefalse

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值