java - How much string objects are created (String.format())? -


after dispute colleague couldn't find solution. question

for(int = 0; < 2; i++) {     string.format("variable = %d", i); } 

how variables created during running code? have opinion, here can created 4 variables:

  1. "variable = %d",
  2. "variable = %d" - on both cycle steps created object formatting
  3. "variable = 0",
  4. "variable = 1" - resulted strings.

am right?

"variable = %d" string literal, put in string pool , not created twice. hence, you'll have total of 3 strings: "variable = %d", "variable = 0" , "variable = 1".


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -