English 中文(简体)
2. 利用新的和与外部方法的比较来制造物体
原标题:String object creation using new and its comparison with intern method
  • 时间:2012-05-13 10:55:01
  •  标签:
  • java
  • string

我在Kathy Sierra书读到,当我们利用新运营商,如Sting s = 新的Sting(“abc”)时, 在本案中,由于我们使用了新的关键词, Java将产生一种正常(非集合)记忆中的新强硬物,并将提到它。 此外,“abc”字面将放在集合中。

括号中说,如果“强食池”已经包含一种扼杀物,那么从集合体中提取的str物将归还,则将“强硬物”添加到集合中,并重新提及这一强硬物。

如果在使用新产品创建时,在集合中加固“ab”,那么主食就是说,如果“斯特林库”包含另外的扼杀物,则从集合中取回。

我也想知道,我们是否利用当时的新东西来创造力量?

最佳回答

http://www.ohchr.org。 如果你真的需要做<条码>新指令”(<>abc”),你就知道你需要并且知道为什么。 说你永远不需要的话,它就很罕见了。 Just use abc”.


长期版本:

您的代码new String ("abc”)时,在不同时间发生下列情况:

  • 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”时,也把str放在集合中,那么,为什么intern(<> >规定,如果“Sting集合”含有另外的扼杀物,则从集合体中提取的str物就在集合中添加。

因为intern。 请注意,打字字字字面上的intern是一个绝版;插字面全自动相互连接。 例如:

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

我也想知道,我们是否利用当时的新东西来创造力量?

您至少创建了一个<代码>String的物体(新的、未加附的物体),可能还有两个物体(如果该字面上已经排在集合体内;但是,在装上字面时,该笔记本再次出现:

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 String("abc”)时,intern(或使用String s = “abc”; 如果有提及“abc”的参考文献,则对固定集合进行检查。

如在集合体和<代码>中已经存在“abc”的提法,请参阅<代码>。 使用<<代码>new String ("abc”)创建的现成物体,然后通过<代码>new String(“abc”)创建。 详见下文守则。

public static void main(String[] args) {
    String s = new String("abc");
    String a = s;
    System.out.println(s==a);// true
    String b = "abc";
    s = s.intern();
    System.out.println(s==a);// false
} 




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签