Videos
You can use new, but it will be a little tricky that you specify using the "intern" method of String. like this:
String a = "ABC";
String b = new String("ABC").intern();
System.out.println(a == b);
Output is true, if no "intern", then it is a copy from the constant pool.
String a = "ABC";
String b = new String("ABC");
System.out.println(a == b);
output is false. If you looks into the prototype of this String constructor, it shows that:
/**
* Initializes a newly created {@code 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 {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
Latter is right. To create a String instance from literal using new() is absolutely meaningless.
Maybe this will aid your understanding:
String literal = "test";
String one = new String(literal);
String two = "test";
System.out.println(literal == two); //true
System.out.println(one == two); //false
In the example you posted:
String one = new String("test");
String two = "test";
the reference passed to the constructor String(String) has the same value as the reference two due to interning. However, the string itself (referenced by these two references) is used to construct a new object which is assigned to reference one.
In this example, there are exactly two Strings created with the value "test": the one maintained in the constant pool and referenced whenever you use the literal "test" in an expression, and the second one created by the "new" operator and assigned to the reference one.
Edit
Perhaps you're confused about this statement:
When the compiler encounters a String literal, it checks the pool to see if an identical String already exists.
Note that this might be more clearly stated as:
When the compiler encounters a String literal, it checks to see if an identical String already exists in the pool.
Strings are only put in the pool when they are interned explicitly or by the class's use of a literal. So if you have, for example, this scenario:
String te = "te";
String st = "st";
String test = new String(te) + new String(st);
then while a String will exist with the value test, said String will not exist in the pool as the literal "test" has never occurred.
//Creates a new object even if one exists in the pool
String s1 = new String("Tendulkar");
// makes a new object string and then the reference is available to the pool
String s2 = s1.intern();
//this object is not created but references the address present in the pool
String s3 = "Tendulkar";
System.out.print(s1==s2); // results in false
System.out.print(s2==s3); //very very true !!!
First of all. String s = new String(“hello”); creates an unnecessary String and should not be used. Next, calling s = s.intern() will ensure that the "hello" added to SCP will be returned and hence the second string that was created on the heap will be eligible for GC.
intern() adds the string to the SCP if it is not already present. It is usually used when you know that a String is used multiple times but you cannot create it using literal. So instead of creating thousands of Strings with the same value, you (which exist simultaneously), you could use intern and ensure that only one String is put in the SCP and is used in 1000 places (and all other strings with the same value on the heap are eligible for GC)
when exactly the object is created in string constant pool when we use new operator.?
It isn't. There is considerable confusion here.
- The object in the string pool is created by the compiler and classloader in response to the use of a string literal, in this case
"hello". - The
newoperator creates a new object, on the heap. - The
intern()method returns a reference to an object in the string pool that either was already there or was created by theintern()call.