How many String objects will be created in String s = "Sachin" + "Tendulkar";

how many String objects are created in the declaration String s = "Sachin" + "Tendulkar" ;? This is my interview question.

0
source share
3 answers
how many String objects are created in the above declaration? 
This is my interview question
  • "Sachin" → String literal

  • "Tendulkar" → String literal

only one String sof the concatenation of two literals is created

+1
source
Strings computed by constant expressions are computed at compile time and then
treated as if they were literals. 

Specifications: here

String s="Sachin"+" Tendulkar";

So, in case you specified , only one string literal (created at compile time ) will be created , and that "SachinTendulkar". Thus, there will only be one interned row in the row pool.

, . .

String s1 = "Sachin";
String s2 = "Tendulkar";
String s3 = s1 + s2;

3 .

+1

Only one for String s="Sachin"+" Tendulkar";

+1
source

All Articles