No, it doesn't. The method documentation says,
Arun Singh Raaj wrote:. . . The explanation says if the String object is not found in Pool . . .
That is different from what you wrote. It looks to see whether there is a String in the pool equal to the current String.If such a String is found, it is substituted for the operand. Otherwise the current String is added to the pool.
if the pool already contains a string equal to this String object
That's a pleasure
Arun Singh Raaj wrote:Thanks Campbell. . . .
Surely you know that already.
Is "s" referencing "Hello" of Pool as well as of main Heap?
That is something you don't actually need to know, but . . .
Arun Singh Raaj wrote:Hello guys,
I'm confused about the information i came through about String;
Some say will create two objects . . . How far is it right?
Where did they say distinct? All objects on the heap are distinct. I think they meant to say, “non‑equal”.
. . . I have read about the intern() method that it makes sure only distinct objects will remain in the memory.
If you use code similar to that above, no. The literal will already exist and intern() will change the reference to point to that particular String object. Then one String object will become eligible for garbage collection (=GC). The rest of the code will determine when the other 999 become eligible for GC.
Does it mean that if I create "Hello" objects using new operator 1000 times and I perform intern() on one of them then rest 999 "Hello" will be destroyed from the Heap?
That's a pleasure
Thanks in advance.
That's a pleasure. Interesting question
Niall Kev wrote:Thank you. . . . Thanks to all for your help
That isn't a correct implementation of a singleton. If you access the factory method from two threads, it is possible for two distinct instances to be returned. The method in line 11 is dangerous; it might return null. I am not convinced that having both methods is consistent with the concept of a singleton at all. I also think that passing a cconfiguration is also inconsistent with the concept of a singleton.
Alex Lee wrote:. . . the singleton object is created independently with 2 created, or they are shared. . . .