使用new创建字符串对象及其与实习方法的比较 [英] String object creation using new and its comparison with intern method

查看:96
本文介绍了使用new创建字符串对象及其与实习方法的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Kathy Sierra的书中读到,当我们使用像String这样的新运算符创建String时s = new String(abc)在这种情况下,因为我们使用了new关键字,Java将在正常情况下创建一个新的String对象(非池) )内存,s将引用它。此外,文字abc将被放置在池中。

I read in Kathy Sierra book that when we create String using new operator like String s = new String("abc") In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, literal "abc" will be placed in the pool.

intern()表示如果String池已包含字符串,则返回池中的字符串否则,将String对象添加到池中并引用该字符串返回String对象。

intern() says that if String pool already contains a string then the string from the pool is returned Otherwise, the String object is added to the pool and a reference to this String object is returned.

如果使用new创建的字符串abc也将字符串放在池中,则wht确实intern()表示池中的字符串是如果String池包含字符串则返回,否则字符串对象被添加到池中。

If string "abc" when created using new also placed the string in the pool, then wht does intern() says that string from the pool is returned if String pool contains the string otherwise the string object is added to the pool.

此外我想知道我们是否使用new创建一个String然后实际获得多少个对象创建?

Also I want to know if we create a String using new then actually how many objects get created?

推荐答案

TL; DR :如果你真的需要做 new String(abc),你知道你需要,你会知道为什么。它是如此罕见,以至于几乎有效地说你永远不需要。只需使用abc

TL;DR: If you ever really need to do new String("abc"), you'll know you need to and you'll know why. It's so rare that it's almost valid to say you never need to. Just use "abc".

长版:

当您拥有代码 new String(abc)时,会在不同时间发生以下情况:

When you have the code new String("abc") the following things occur at various times:


  • 当加载包含该代码的类时,如果字符串abc尚未进入实习池,它已创建并放在那里。

  • 当运行新String(abc)代码时:

    • 来自实习池的abc字符串的引用传递到字符串构造函数。

    • new 字符串对象已创建并初始化为复制传递给构造函数的 String 中的字符。

    • 字符串对象返回给你。

    • When the class containing that code is loaded, if a string with the characters "abc" is not already in the intern pool, it's created and put there.
    • When the new String("abc") code is run:
      • A reference to the "abc" string from the intern pool is passed into the String constructor.
      • A new String object is created and initialized by copying the characters from the String passed into the constructor.
      • The new String object is returned to you.

      如果字符串abc在使用new创建时也将字符串放在池中,那么为什么 intern()表示如果String池包含字符串,则返回池中的字符串,否则将字符串对象添加到池中。

      If string "abc" when created using new also placed the string in the pool, then why does intern() says that string from the pool is returned if String pool contains the string otherwise the string object is added to the pool.

      因为那是实习生所做的。请注意,在字符串文字上调用实习生是一个无操作;字符串文字都是自动实现的。例如:

      Because that's what intern does. Note that calling intern on a string literal is a no-op; string literals are all interned automatically. E.g.:

      String s1 = "abc";               // Get a reference to the string defined by the literal
      String s2 = s1.intern();         // No-op
      System.out.println(s1 == s2);    // "true"
      System.out.println(s1 == "abc"); // "true", all literals are interned automatically
      




      我也想要要知道我们是否使用new创建一个String然后实际创建了多少个对象?

      Also I want to know if we create a String using new then actually how many objects get created?

      你创建至少一个 String object(新的,非实习的),可能还有两个(如果文字不在池中;但是再次,当加载类文件的文字时,这个位发生得更早):

      You create at least one String object (the new, non-interned one), and possibly two (if the literal wasn't already in the pool; but again, that bit happens earlier, when the class file's literals are loaded):

      String s1 = "abc";            // Get a reference to the string defined by the literal
      String s2 = new String(s1);   // Create a new `String` object (guaranteed)
      System.out.println(s1 == s2); // "false"
      String s3 = s2.intern();      // Get the interned version of the string with these characters
      System.out.println(s1 == s3); // "true"
      

      这篇关于使用new创建字符串对象及其与实习方法的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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