简体   繁体   中英

When exactly the object is created in string constant pool when we use new operator.?

String s = new String(“hello”); 

Here two objects will be created, one in heap memory and another in the string pool.

So, what is the use of the intern() method? The string "hello" will be available in heap as well as the string pool after above statement execution

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.

  1. 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" .
  2. The new operator creates a new object, on the heap .
  3. The intern() method returns a reference to an object in the string pool that either was already there or was created by the intern() call.

An object is created in the string constant pool if anything is written in double quotes and if it doesn't already exists in the string constant pool.
As for intern() method it returns the canonical representation of string. For further understanding see
http://www.javatpoint.com/java-string-intern

what is the use of the intern() method

intern strings gives the simplicity to compare strings with == (faster) instead of equals function where non-intern can't use the == operator for equality.

String s = new String(“hello”); 

new will assign memory to s in heap instead of internal set of unique strings which is maintained by VM ,also known as SCP .All strings found in class at the time of loading calls, are automatically interned(with strong-reference) which leads to efficient memory use.

Calling intern() on s string literal will add a weak-reference(short-time) of s in SCP and also returned that reference so GC will surely free heap memory consumed by s .

Weak-reference will also be deleted when it is no longer used hence again leads to efficient memory management.

When exactly the object is created in string constant pool

String will be added to SCP temorarly, either with direct double quotes (String s="sytax";) syntax or calling intern() .

when we use new operator?

Avoid it as much as you can with strings or never.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM