垃圾收集和字符串 [英] Garbage collection and Strings

查看:141
本文介绍了垃圾收集和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对字符串有疑问

它们是否存在于堆或字符串池中?

Are they live on Heap or String pool?

如果在Heap然后它们将被垃圾收集,如果任何活动线程都无法访问它们。

And if on Heap then they will be garbage collected, if they are not reachable by any live thread.

如果在字符串池中那么它们将如何被删除或删除因为我们知道垃圾收集只发生在堆上。

And if on String pool then how they will be deleted or removed because as we know Garbage Collection happens only on heap.

推荐答案

String s = new String("abc");

s引用的字符串对象将在堆上,字符串文字abc将在字符串中池。字符串池中的对象不会被垃圾回收。它们可以在程序的生命周期中重复使用,以提高性能。

the string object referred by s will be on heap and the string literal "abc" will be in string pool. The objects in the string pool will not be garbage collected. They are there to be reused during the lifetime of the program, to improve the performance.

这篇关于垃圾收集和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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