-1
class Help {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello" + "world";
    }
}
  • Is s1 here an object reference variable, or did we create an object in the first statement?
    • If answer is yes, then in the second statement how many objects will be created ?
azro
  • 53,056
  • 7
  • 34
  • 70
Tushar Mia
  • 375
  • 2
  • 9
  • Related: [What is the difference between “text” and new String(“text”)?](https://stackoverflow.com/q/3052442) – Pshemo Dec 22 '18 at 14:12
  • 2
    In short since `"hello"` and `"world"` are *compilation time constants* which value is known at compilation time, compiler will do concatenation and compile `"hello"+"world"` as if it was written like `"helloworld"`. – Pshemo Dec 22 '18 at 14:14

1 Answers1

1

No. You will create a new String object always when you use new operator. If you create a String normally without new operator it will return an existing object from String pool if it is already existing, if its not it will create a new one

Refer this example for more info

Sandeepa
  • 3,457
  • 5
  • 25
  • 41