The compiler sees that c and d are final. This means that the compiler knows that c and d will never change. So it compiles the code
String g = "CD"; String h = "CD";
g and h, therefore, are two references to the same literal STring that is interned.
He cannot optimize e and f in the same way, because a and b are not final and can change.
source share