Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • String s1 = new String("abc"), String s2 = new String("abc"). s1 != s2, this is because the two object is different. But in memory there are one copy of 'abc' or two? where dose jvm allocate the 'abc' when it's created by constructor. Commented May 9, 2016 at 7:47
  • In most cases (when the size of the String and the underlying char array are equal), the new String object will have the same underlying char array as the passed String object. So there is one copy of 'abc' in memory (represented as a char array), but two strings using this. Commented May 10, 2016 at 2:22
  • 1
    This answer is simply wrong, so the upvotes should be removed. The construct new String("word") would only create a new string in the pool if there was no string literal in the pool with the same value. It will however create a new String object that references any existing literal in the pool, hence the result of checking for object reference equality. Commented May 27, 2016 at 1:32
  • I clarified the answer. It was correct before, you misread it. Commented May 28, 2016 at 10:23