There are two ways to create string in java as below:
- String s = new String("Hellow");
- String s1 = "Hellow";
In case 1, there are two objects(one in String constant pool and another one in Heap memory) which will be created while in case 2, only one object will be created in String constant pool only.
As per my understanding, whenever we are creating object using new keyword one object is also getting created in string constant pool. So why to create a string using new keyword, instead we should always create via string literal.
So could you guys please let me know if my understanding is right or wrong. And if it is wrong, please provide the scenario where it is must to create the string using "new".