在 Java 中使用 String 实习生方法时创建的对象数 [英] Number of objects created when using String intern method in Java

查看:56
本文介绍了在 Java 中使用 String 实习生方法时创建的对象数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 String 的实习生方法.

I understand String's intern method.

String s1 = "Hello";             
String s4 = new String("Hello").intern();  

(s1 == s4) 的输出将是 true,如果我们没有使用 intern,它将是 false代码>.

Output of (s1 == s4) will be true, it would be false had we not used intern.

我的问题是执行上面两条语句,会创建多少个对象??一个或两个?new 运算符会再创建一个对象吗?

My question is on executing the above two statements, how many objects will be created?? One or two? Will new operator creates one more object?

我知道 String s4 = new String("Hello") 将创建两个对象,但对使用 intern 和它感到困惑.

I understand that String s4 = new String("Hello") will create two objects, but got confused with using intern with it.

推荐答案

会有两个

一个

String s1 = "Hello"; 

另一个

String s4 = new String("Hello")

这篇关于在 Java 中使用 String 实习生方法时创建的对象数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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