我们编写时创建了多少个对象 [英] How many objects are created when we write

查看:98
本文介绍了我们编写时创建了多少个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String str=new String ("xyz");

我读到某个地方,JVM在池中第一个创建了2个对象,在堆中创建了第二个对象.

I read somewhere JVM create 2 object 1st in pool and 2nd in heap.

这是真的吗?如果为true,那么为什么JVM在已经存在2个对象的情况下创建2个对象,有人可以解释吗?

Is this true ? If true then why JVM create 2 object when one is already there can anyone explain?

推荐答案

是的,您是正确的.它创建两个对象.在字符串常量池中一个,在堆中另一个对象指向字符串池.

Yes, you are right. It creates two objects. One in String Constant Pool and another object in Heap pointing to the String pool.

  1. 如果我们考虑String str ="Hello"//String文字,它将由JVM在String Constant Pool中仅创建一个对象.
  2. 按照您的语法(String str = new String("xyz")//String对象),JVM创建2个对象.一个在字符串池中,另一个在堆中).

有关更多参考,请检查以下讨论: 字符串s =新字符串("xyz").在执行这行代码之后,已经制作了多少个对象?

For further reference, please check the following discussion: String s = new String("xyz"). How many objects has been made after this line of code execute?

这篇关于我们编写时创建了多少个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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