3

Let's say that I need to iteratively retrieve a value of the same key from a 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")? I was wondering if it's always better to have a String constant, or it doesn't matter.

pandagrammer
  • 841
  • 2
  • 12
  • 24
  • 3
    String interning; it *is* a constant. – Dave Newton Jul 11 '14 at 19:16
  • damn, i missed this. easy points!! -) – T McKeown Jul 11 '14 at 19:17
  • Strings are constants and once created are being stored in so called "string pool". You can read more about it here: [Java String Pool](http://stackoverflow.com/questions/2486191/java-string-pool). But some may quarrel that it's a good practice to extract the commonly used string to a constant. – iluu Jul 11 '14 at 19:17

3 Answers3

4

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

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
resueman
  • 10,572
  • 10
  • 31
  • 45
  • i don't think s3 interned automatically. – Vipin Jul 11 '14 at 20:12
  • No. The String literal used in the constructor will be, but the final Object assigned to s3 won't be unless you explicitly intern it. – resueman Jul 11 '14 at 20:13
  • Then you should correct your answer also. These are not interned automatically. – Vipin Jul 11 '14 at 20:22
  • @Vipin But s3 is being created with the Object constructor, which I never say is being interned automatically. Only String literals are. – resueman Jul 11 '14 at 20:24
  • Actually when u say "No" very first word I assume answer to the question is always No. And "String constant" comes just after No so I was referring it with all String objects (prepared by any way). – Vipin Jul 11 '14 at 22:06
2

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. It is not internedn(Doesn't go to String pool) so you can not take it back from memory.
  2. Concatenation ( "a" + "b" ) always creates new String Object and it is not interned (Doesn't go to String pool).

No Cases

  1. String 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(); if already available it retrieves from pool , when not available it creates new object which is interned also (Goes to String pool as well).
Vipin
  • 4,851
  • 3
  • 35
  • 65
1

is the "KEY" string created every time I call map.get("KEY")?

No.

Java Strings are immutable, which allows the Java compiler to use a single instance for all string literals. 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 s1 = "bla";
String s2 = "bla";
// s1 == s2 
String s3 = new String ("bla");
// s1 != s3 
Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30