简体   繁体   English

每次我使用String时,它是否会创建一个新的String对象?

[英]Every time I use String, does it create a new String object?

Let's say that I need to iteratively retrieve a value of the same key from a Java hashmap. 假设我需要从Java hashmap迭代地检索相同键的值。

  for(int i=0; i<INTEGER.MAX; i++)   
          map.get("KEY");

In this case, is the "KEY" string created every time I call map.get("KEY")? 在这种情况下,每次调用map.get(“KEY”)时都会创建“KEY”字符串吗? I was wondering if it's always better to have a String constant, or it doesn't matter. 我想知道是否总是更好地拥有一个String常量,或者无关紧要。

No. String constants are interned automatically, so any identical string literals all reference the same object in memory. 不会。字符串常量会自动实现,因此任何相同的字符串文字都会引用内存中的相同对象。

Some more information on this: http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3 关于此的更多信息: http//www.xyzws.com/Javafaq/what-is-string-literal-pool/3

An example of this: 一个例子:

String s1 = "Test";
String s2 = "Test";
String s3 = new String("Test");
s1 == s2;//Evaluates to true, because they are the same object (both created with string literals)
s1 == s3;//Evaluates to false, because they are different objects containing identical data

Yes/No Answer depends on how you create String Objects. 是/否答案取决于您如何创建字符串对象。 Below are the four scenarios I can think of as of now. 以下是我现在可以想到的四种情景。

Yes Cases 是的案例

  1. new String() always creates new Object. new String()总是创建新的Object。 It is not internedn(Doesn't go to String pool) so you can not take it back from memory. 它不是internedn(不会转到String池)所以你不能从内存中取回它。
  2. Concatenation ( "a" + "b" ) always creates new String Object and it is not interned (Doesn't go to String pool). 连接(“a”+“b”)总是创建新的String对象并且它没有被实现(不会转到String池)。

No Cases 没有案例

  1. String a ="aa"; 字符串a =“aa”; if already available it retrieves from the pool, when not available it creates a new object which is interned also (Goes to String pool as well) 如果已经可用,则从池中检索,当它不可用时,它会创建一个也被实习的新对象(也可以转到字符串池)
  2. new String().intern() or "aa".intern() ; new String()。intern()或“aa”.intern() ; if already available it retrieves from pool , when not available it creates new object which is interned also (Goes to String pool as well). 如果已经可用它从池中检索,当它不可用时,它会创建一个也被实习的新对象(也可以转到字符串池)。

is the "KEY" string created every time I call map.get("KEY")? 是每次调用map.get(“KEY”)时创建的“KEY”字符串?

No. 没有。

Java Strings are immutable, which allows the Java compiler to use a single instance for all string literals. Java字符串是不可变的,它允许Java编译器对所有字符串文字使用单个实例。 That is: all identical string literals in your program will reference a single string object. 也就是说:程序中所有相同的字符串文字都将引用单个字符串对象。

In the rare cases you need identical strings to be wrapped in two separate objects, you must explicitly instantiate a String object: 在极少数情况下,您需要将相同的字符串包装在两个单独的对象中,您必须显式实例化String对象:

String s1 = "bla";
String s2 = "bla";
// s1 == s2 
String s3 = new String ("bla");
// s1 != s3 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ + =”运算符是否总是创建一个新的字符串对象? - Does `+=` operator always create a new string object? 每次单击按钮时,Firebase 都会创建一个新对象 - Firebase create a new object every time I click a button 为什么jvm每次使用new关键字创建字符串时都会创建新的字符串Object - Why jvm create new string Object each time we create string using new keyword 休眠是重用存储在内存中的对象还是每次都创建新对象? - Does hibernate reuse object stored in memory or does it create new object every time? 每次创建新的不同类的对象 - Create Object of new different Class every time 当我们在字符串上调用toString时,它会创建一个新对象吗? - Does toString create a new object when we call it on a String? How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? - How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? StringBuilder是否在每个操作中创建一个新的String? - Does StringBuilder creates a new String in every operation? Java - 当新字符串等于旧字符串时,为什么 substring() 不创建新对象? - Java - Why does substring() not create a new object when the new string is equal to the old string? 每次限制字符串创建对象 - Restrict string creating object every time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM