1
String str = new String(“my literal”);

In above statement ,two objects would be created ,one as String literal “my literal” in string constant pool (If it's not present in string pool) and other in heap area as object String(“my literal”)

Q-1 I know the benefit of putting the string literal in string pool area but I am not able to think of about the benefit of creating a duplicate object in heap?

Q2- As I read in some stack over flow link: If use of new String("my literal") is almost always bad because you will be creating 2 Strings one on String constants pool and another on heap with the same value ,then my question is why does Java creates duplicate object in heap? Why not java just ignore creating in heap?

amarnathpatel
  • 981
  • 10
  • 20
  • @Sotirios Although it may looks duplicate but its not actually duplicate ,the link provided does not have answer of my question " about the benefit of creating a duplicate object in heap?" – amarnathpatel Nov 05 '15 at 17:30
  • 1
    If you scroll down to [neeraj's answer](http://stackoverflow.com/a/29535476/438154), you'll notice this quote from the javadoc: _Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable._ That's all there is to it. – Sotirios Delimanolis Nov 05 '15 at 17:39
  • Thanks Sotirios but why does Java creates duplicate object in heap in this case ? Why not java just ignores creating object in heap as its already present in pool? – amarnathpatel Nov 05 '15 at 17:42
  • 3
    Because you explicitly used `new`. – Sotirios Delimanolis Nov 05 '15 at 17:42
  • @amarnathpatel Why do you think there are two objects, the value will only be added to the constant pool when you call str.intern(), otherwise not. – User2709 Nov 05 '15 at 17:47
  • @Sotirios So basically there is no benefit or use of this duplicate object in heap(and its like a overhead in case of String as in the example given in this question) and it's just created because of explicitly using new keyword . Please confirm if its correct understanding. – amarnathpatel Nov 05 '15 at 17:47
  • @amarnathpatel, that would be very confusing as in any other case you can be sure the reference returned from `new` is unique. If JDK starts optimizing that and return an instance that can be possibly held by other thread, it can break existing code that uses that object as for synchronization. – Oliver Gondža Nov 05 '15 at 17:50
  • Have you seen this constructor used in real code? Or has someone told you there is a benefit to this? Just trying to understand why you are asking. – Klitos Kyriacou Nov 05 '15 at 18:02
  • @Klitos No ,i have not seen in real code actually but this was once asked by an Java interviewer. – amarnathpatel Nov 18 '15 at 06:06

1 Answers1

5

There is almost no benefit to calling the String(String) constructor with a literal string. There used to be a significant benefit to calling the String(String) constructor with a different string expression.

The literal is already a String, and String objects are immutable. More generally, for any String expression passed to the String(String) constructor, the constructor is usually unnecessary, because the argument is already an immutable String.

From the String(String) constructor documentation:

public String(String original)

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

With older versions of Java (prior to 1.7.0_06), the String(String) constructor was more useful. A String created by String.substring() could refer to the larger original String, and prevent it from being garbage collected. The String(String) constructor cut the ties to the larger String.

You asked:

Q-1: ... The benefit of creating a duplicate object in heap?

Usually there is none. However, if you're using a String object in a context where object identity matters, you might want a different object. For example:

  • You're using String objects as keys within a IdentityHashMap, and want only your own String objects to match.
  • You want to synchronize on a string value provided by external code. You don't want any other code synchronizing on the same String object; it could lead to unexpected interference and deadlock. [This example provided by @biziclop in a comment below.]

Q-2: ... why does Java creates duplicate object in heap?

Because you explicitly asked it to with new String("my literal"). If you use the new operator, you get a new object.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • I think you mean `IdentityHashMap`. The answers you've given to their questions are repeated from the duplicate post. You should've just written this answer there. – Sotirios Delimanolis Nov 05 '15 at 17:51
  • Thanks, I did mean that. I looked at the post you cited *and* searched for other existing duplicates, and did not see the answer to this question. – Andy Thomas Nov 05 '15 at 17:55
  • 1
    Another area where object identity matters is synchronization. Though you'd be quite foolish to synchronize on a `String` handed to you by some external code, creating a duplicate would ensure there's no interference. – biziclop Nov 05 '15 at 18:07
  • @biziclop - Good example. I've added it to the answer above, with attribution. Thanks! – Andy Thomas Nov 05 '15 at 18:19