堆内存和字符串池之间的区别 [英] Difference between heap memory and string pool

查看:57
本文介绍了堆内存和字符串池之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中堆内存和字符串池有什么区别?

What is the difference between heap memory and string pool in Java?

in this link , it is said that:

String s1 = "Hello";

String s2 = new String("Hello");

s1 points to String Pool's location and s2 points to Heap Memory location.

解决方案

StringPool is an area that the JVM uses to avoid redundant generation of String objects..

Those objects there can be "recycled" so you can (re)use them and so avoiding the "waste" of too much memory...

Consider the following example:

String s1 = "cat";

String s2 = "cat";

String s3 = new String("cat");

the JVM is smart enough to see that the object s2 is going to be assigned with the value "cat" which is already allocated in the memory(and assigned to the object "s1"), so instead of creating a new object and wasting that new memory place, it assign the reference to the same memory allocated for s1

这篇关于堆内存和字符串池之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆