0

We use intern() method for the Strings which are created through new String() so that they will create an entry in String Pool and return the newly created String from the String Pool so that this created string is eligible for use with == operator (as per my understanding).

Then what is the use of creating a new String through constructor?

When should we use constructor for creating new String?

C Snover
  • 17,908
  • 5
  • 29
  • 39
Aalekh
  • 400
  • 2
  • 7
  • 12
  • Have a look at this [string constructor use](http://stackoverflow.com/questions/465627/use-of-the-stringstring-constructor-in-java) – Arkantos Feb 08 '15 at 16:43
  • 3
    You're abusing the intern() method. Just never use `==` with Strings, and never use the String constructor taking a String as argument. – JB Nizet Feb 08 '15 at 16:46
  • There is also a (much more useful) String constructor that takes a byte array to create it - though I'm not sure if you would need to call `intern()` on that as well. – Luan Nico Feb 08 '15 at 17:58
  • @JBNizet Sometimes there are valid reasons for testing reference equality (e.g., using a distinguished String reference as a sentinel value). But yeah, don't use '==' to test whether two strings have the same _value_. – Solomon Slow Feb 08 '15 at 23:39

2 Answers2

-1

new String() can create at-most two Objects of String and at-least one. one in constant pool (if same is not present in constant pool) and another in heap memory. constant pool entry is created by interning String object.

intern will create String Literal in Constant pool so that whenever you create String without new keyword it will not create string object.

suppose you have created as

String str= new String("abc");

two object will be created one in constant pool (if "abc" is not present in constant pool). jvm wil internally call intern for that object. so next time if you are doing

String str1= "abc";

no entry will be added to constant pool because only entry can be possible in constant pool for same literal.

Prashant
  • 2,556
  • 2
  • 20
  • 26
  • How does this answer the question about "when to use the String constructor"? – Tom Feb 08 '15 at 18:09
  • The String constructor only creates _one_ string in your example. The String literal, `"abc"` is not created by the `new String("abc")` call, nor even by the statement containing that call. It is created when the class file that contains that statement is first loaded into memory. – Solomon Slow Feb 08 '15 at 23:45
  • @jameslarge : you can check study material. JVM is mostly calling intern() for required String object. – Prashant Feb 09 '15 at 05:12
  • @Tom : if you want to compare String object on the basis of content then you can String constructor. and downvote for this?? – Prashant Feb 09 '15 at 05:14
  • @Prashant And if I don't want to compare Strings? Then what? And you should ask the downvoter for his/her reason, not me. – Tom Feb 09 '15 at 07:43
  • Yes, the JVM interns String literals that appear in your program, but you said, "new String() can create at-most two Objects of String..." That's not true. The constructor call only creates one String object. The literal String "abc" in your example existed _before_ the constructor was called. – Solomon Slow Feb 09 '15 at 11:57
  • @jameslarge below i have written that also. and i didn't said new String() will call intern method. so i don't think that is valid for downvoting. – Prashant Feb 09 '15 at 12:06
-1
public native String intern(); java doc says, 

A pool of strings, initially empty, is maintained privately by the class >{@code String}. When the intern method is invoked, if the pool already >contains a string equal to this {@code String} object as determined by >the {@link #equals(Object)} method, then the string from the pool is >returned. Otherwise, this {@code String} object is added to the pool and >a reference to this {@code String} object is returned. It follows that >for any two strings {@code s} and {@code t}, {@code s.intern() == >t.intern()} is {@code true} if and only if {@code s.equals(t)} is {@code >true}.

Let's consider an example:

String temp1 = new String("abcd"); this means, a new object "abcd" will be created in the heap memory and its reference will be linked to temp1.

String temp2 = new String("abcd").intern(); this means "abcd" literal will be checked in the String Pool. If already present, its reference will be linked to newly created temp2. Else a new entry will be created in String Pool and its reference will be linked to newly created temp2.

String temp3 = "abcd"; this means "abcd" literal will be checked in the String Pool. If already present, its reference will be linked to newly created temp2. Else a new entry will be created in String Pool and its reference will be linked to newly created temp3. This conclude that, Java automatically interns String literals.

Let's consider another example:

String s1 = "string-test";

String s2 = new String("string-test");

String s3 = new String("string-test").intern();

if ( s1 == s2 ){
    System.out.println("s1 and s2 are same");
}

if ( s2 == s3 ){
    System.out.println("s2 and s3 are same" );
}

if ( s1 == s3 ){
    System.out.println("s1 and s3 are same" );
}

Output: s1 and s3 are same

Now when to create a String object using = " " or using new String(" ")?

One use case which I came across,

// imagine a multi-megabyte string here String s = "0123456789012345678901234567890123456789"; String s2 = s.substring(0, 1); s = null;

You'll now have a String s2 which, although it seems to be a one-character string, holds a reference to the gigantic char array created in the String s. This means the array won't be garbage collected, even though we've explicitly nulled out the String s!

The fix for this is to use String constructor like this:

String s2 = new String(s.substring(0, 1));

Ankur Piyush
  • 308
  • 1
  • 9