这个生成了多少个Java对象 - 新的String(“abcd”) [英] How many Java objects are generated by this - new String("abcd")

查看:137
本文介绍了这个生成了多少个Java对象 - 新的String(“abcd”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String s = new String("abcd");


推荐答案

实习池中有一个字符串,它将是每次运行代码时都重复使用。

There's one string in the intern pool, which will be reused every time you run the code.

然后每次运行该行时都会构造额外的字符串。例如:

Then there's the extra string which is constructed each time you run that line. So for example:

for (int i = 0; i < 10; i++) {
    String s = new String("abcd");
}

最终会有11个字符串,其内容为abcd - 内存中实习1和10份。

will end up with 11 strings with the contents "abcd" in memory - the interned one and 10 copies.

这篇关于这个生成了多少个Java对象 - 新的String(“abcd”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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