Search code examples
javastringstring-pool

How may String Objects are created in java (By the following code)


How many String objects are created in java by the following code: if there is no String object in the String pool containing the same value . (I read somewhere that Since we are passing arguments as "Hello", which is a String literal, it will also create another object as "Hello" on string pool. )

String s="Hello";

Solution

  • Only one object will be created in the string constant pool. Reason behind is while we creating the object,we didn't use any "new" keyword.