0

Recently i was reading i got this

"When you declare a String (which is immutable) variable as final, and initialize it with a compile-time constant expression, it also becomes a compile-time constant expression, and its value is inlined by the compiler where it is used."

and "i'm really confused what does this mean its value is inlined by compiler" ? Please explain it in Simple way if possible

Source of the above line

Community
  • 1
  • 1

1 Answers1

2

When a String is finalized and initialized at compile time, the compiler can copy-paste the string into the code, instead of looking up the variable at every use. Similar to inline expansion.

final String a = "asd";
String b = a;

The above snippet becomes

final String a = "asd";
String b = "asd";
EvenLisle
  • 4,672
  • 3
  • 24
  • 47