如何通过System.out.println("Morning")创建可访问的对象? [英] how is the object created via System.out.println("Morning"), REACHABLE?

查看:41
本文介绍了如何通过System.out.println("Morning")创建可访问的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下语句中

System.out.println("Morning");

在字符串池中创建包含值"Morning"的对象.而且我们没有明确存储它的地址.但是这个对象仍然可以到达.

the object containing the value "Morning" is created inside the String Pool. And we are not storing it's address explicitly. But still this object is reachable.

如果我们尝试执行新的 System.out.println("Morning"); ,则JVM将重复使用先前创建的对象(因为它存在于字符串池中),因此不会创建新对象对象.

If we try to do anew System.out.println("Morning"); the JVM reuses the previously created object as it is existing in the string pool, it doesn't create a new object.

即使我们没有显式存储它的地址,JVM如何跟踪该对象?

How does JVM keep track of this object , even though we are not storing it's address explicitly?

推荐答案

早晨" 是字符串文字.加载类时,所有字符串文字都将添加到字符串常量池中.

"Morning" is a String Literal. All String Literals are added to the String constants pool when the class is being loaded.

我们并没有明确存储它的地址

And we are not storing it's address explicitly

是否存储引用与将String常量添加到常量池无关." 中的所有内容均已保存.

Whether we store the the reference or not has nothing to do with adding String literals to the constants pool. Everything within "" goes there.

但是这个对象仍然可以到达.

But still this object is reachable.

同样,可到达性与添加和检索String常量池无关.JVM的工作方式可能特定于JVM,但某些JVM可能会在类加载期间将类常量字符串添加到字符串常量池中.

Again, reachability has nothing to do with adding and retrieving from the String constants pool. How the JVM does it could be specific to the JVM but some JVMs could add Class constants Strings to the String constant pool during class loading.

这篇关于如何通过System.out.println("Morning")创建可访问的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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