will two strings with same content be stored in the same memory location?

前端 未结 9 1159
慢半拍i
慢半拍i 2020-11-27 19:29

This is a question that I got in an interview.

I\'ve two strings defined as

String s1=\"Java\";
String s2=\"Java\";

My question i

9条回答
  •  眼角桃花
    2020-11-27 20:11

    Adding to others: new keyword always forces to create a new object. If you declare like below:

    String s1 = "some";
    String s2 = "some";
    

    Then using String Pooling mechanism, both references s1 and s2 will refer to the same String object with the value "some".

提交回复
热议问题