English 中文(简体)
Java:在2个班次中,只有2个字面(相同内容)使用同样的隔 the?
原标题:Java:how come 2 literals(the same contents) in 2 classes use the same interned string?
  • 时间:2012-05-20 13:02:05
  •  标签:
  • java
  • string
class A {
    String s = "abc";
}

class B {
    String s = "abc";

     static public void main(String[]args ) {
            A a = new A();
            B b = new B();
            System.out.println("a.s==b.s : " + (a.s==b.s));
     }
}

产出:

a.s==b.s : true

If the literal is written to the "constant pool" part of the classfile,how come 2 literals(the same contents) in 2 classes use the same interned string? How does the compiler link them together?

最佳回答

座标有,显示所有相联的<代码>。 有效使用<代码>HashMap<String>。 请注意,<代码>String.intern() 方法回归String;执行基本是

if (stringPool.contains(this))
    return stringPool.get(this);
else {
    stringPool.put(this, this);
    return this;
}

当在装货单时,用电话intern(>取代,其结果就是在所有班级之间分享类似情况。

问题回答

这正是:

此外,插图总是指同一类强盗。 这是因为,用“Sting.intern”的方法,描述字面——或者更概括地说,描述常识的价值观(第15.28段)——被“调和”,以便分享独特的事例。

例如,解释包括:

不同班级的校服同样也指同种物。

String.intern() 如同以下文件一样:

归还直观物体的名声。

一组原为空的护卫由“斯特林”级保持。

如果使用永久方法,如果该集合已经含有与按等值(目标)方法确定的固定物体相等的体积,那么从该集合中提取的体积就会回来。 否则,将在本库中添加这一 object物,并退回提及该 object物。

因此,对于任何两条扼杀物,如果并且只有当平等(t)是真实的,则“intern()”=t.intern()。

所有字面插图和直观价值不变的表述都受到干扰。 粗略字体在 Java门语言规格第3.1.5节中界定。

所有这些都表明你在你的例子中看到的行为。

缩略语 所有拥有相同负荷的<代码>标准级的班次都将看到同一间座标。 由于<编码>String由根类载体之一装载,因此,在单单单单单单单单单单单单单单单单单单体照相。





相关问题
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 ...

热门标签