堆内存和字符串池之间的区别 [英] Difference between heap memory and string pool
问题描述
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屋!