String s = new String(" xyz")。执行这行代码后,有多少个对象被创建? [英] String s = new String("xyz"). How many objects has been made after this line of code execute?

查看:105
本文介绍了String s = new String(" xyz")。执行这行代码后,有多少个对象被创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个采访问题的共同商定答案是代码创建了两个对象。但我不这么认为;我写了一些代码来确认。

The commonly agreed answer to this interview question is that two objects are created by the code. But I don't think so; I wrote some code to confirm.

public class StringTest {
    public static void main(String[] args) {
        String s1 = "a";
        String s2 = "a";
        String s3 = new String("a");
        System.out.println("s1: "+s1.hashCode());
        System.out.println("s2: "+s2.hashCode());
        System.out.println("s3: "+s3.hashCode());
    }
}

输出为:

这是否意味着只创建了一个对象?

Does this mean that only one object was created?

重申:我的问题是以下代码创建了多少个对象:

Reaffirm: My question is how many object was created by the following code:

String s = new String("xyz")

而不是 StringTest 代码。

受到@Don Branson的启发,我调试了下面的代码:

Inspired by @Don Branson, I debugged the below code:

public class test {
    public static void main(String[] args) {
        String s = new String("abc");
    }
}

结果是:

s的id为84,abc的id为82.这究竟是什么意思?

The id of s is 84, and the id of "abc" is 82. What exactly does this mean?

推荐答案

< h2>除了您使用的JVM / JRE之外,还有哪些错误。更好的是不要担心像这样的事情。请参阅评论部分以了解任何纠正/关注。

首先,这个问题确实在这里提出了这个问题:
String Literal Pool是对字符串对象,或对象的集合

因此,这是每个人在此问题上的指南。

So, that is a guide for everyone on this matter.

...

有两种方法可以看这个:

There are two ways of looking at this:

(1)当代码执行 - 它在程序中运行的字面时刻?

(1) What happens when the line of code executes -- the literal moment it runs in the program?

(2)多少对象是由声明创建的?

(2) What is the net effect of how many Objects are created by the statement?

a)xyz 字符串是在JVM加载时创建并实现的,这行代码包含在。

a) The "xyz" String is created and interned when the JVM loads the class that this line of code is contained in.


  • 如果xyz已经在其他人的实习池中代码,那么文字可能不会产生新的字符串对象。

  • If an "xyz" is already in the intern pool from some other code, then the literal might produce no new String object.

b)当创建新的 String s 时,内部 char [] 是实习的副本xyz string。

b) When new String s is created, the internal char[] is a copy of the interned"xyz" string.

c)这意味着,当行执行时,只有一个额外的对象创建。

c) That means, when the line executes, there is only one additional object created.

事实上,只要加载了类并且在此代码之前,就会创建xyz对象部分曾经运行。

The fact is the "xyz" object will have been created as soon as the class loaded and before this code section was ever run.

...下一个场景......

...next scenario ...

String s1 = "a";
String s2 = "a";
String s3 = new String("a");

a)s1和s2只是被引用,而不是对象,它们指向相同的字符串在内存中。

a) s1 and s2 are just referenced,not objects, and they point to the same String in memory.

b)a是实例并且是复合对象:一个 char [] object和 String 对象本身。它由内存中的两个对象组成。

b) The "a" is interned and is a compound object: one char[] object and the String object itself. It consisting of two objects in memory.

c)s3, new String(a)再产生一个宾语。新的字符串(a)不会复制a的 char [] ,它只引用它内部。以下是方法签名:

c) s3, new String("a") produces one more object. The new String("a") does not copy the char[] of "a", it only references it internally. Here is the method signature:

public String2(String original) {
        this.value = original.value;
        this.hash = original.hash;
}

一个实习字符串 (a)等于2 对象。一个 new String(a)等于另一个对象。代码的净效应是三个对象。

One interned String ("a") equals 2 Objects. And one new String("a") equals one more object. Net effect from code is three objects.

这篇关于String s = new String(&quot; xyz&quot;)。执行这行代码后,有多少个对象被创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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