Question: Hi i am working in java and want to know how String objects are created in the String pool
 and how they are managed.

So in the following example i am creating two Strings s and s1,so can anyone explain me how many Objects are created in LIne1?Also how many Objects are eligible for garbage collection in Line3?

```
  String s = "x" + "y";//Line 1
  String s1 = s;//Line 2
  s = null;//Line 3

```

Answer: 
Only one object is created `"xy"` . compiler does it for optimization.

No object is eligible for garbage collection.
It would create one object `xy` in the String constant pool area. As `"x"+"y"` would be evaluated during compilation. Additionally, garbage collector cannot access String constant pool area.

Reference: <https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5>