Reficio™ - Reestablish your software!

String literal pool and JLS rules for String objects

I have been playing with the String objects in order to review all the rules defined by the JLS regarding the String creation and handling. I have recently created a JUnit test case which examines all the aforementioned rules. Enjoy!

JLS rules for Strings:

  • Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
  • Literal strings within different classes in the same package represent references to the same String object.
  • Literal strings within different classes in different packages likewise represent references to the same String object.
  • Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
  • Strings computed by concatenation at run time are newly created and therefore distinct.

Other rules:

  • Strings from the string pool are not eligible for garbage-collection since JVM maintains a single reference to each unique String object from the String pool (Subject of discussion: LINK)
  • new operator does not put newly created string into the string pool
  • From the javadoc: “String.intern() returns a canonical representation for the string object. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

All test methods are presented below. The code compiles and runs without errors.


  1. Step Parents says:

    Hi there it’s me, I am also visiting this site regularly, this web site is in fact good
    and the users are truly sharing good thoughts.

  2. Burn At Work says:

    Just like the Lοs Angeles ѡay of life, Misѕ Me offers ʏou а diversee
    ɑnd stunning array of clothing. Meta-Resilience iѕ like tɦat – something աhich binds
    us and ρrovides սs with a definition aand identity.
    Τhey аre maԀe on thе basis oof tɦe trend in our careers.

Speak Your Mind