How does weak Reference work in Java

Hi,
I have the following code :

public class Test {
  
  public static void main(String[] args) throws InterruptedException {
  
      String s = "test";
    
    WeakReference<String> s2 = new WeakReference<String>("test");
       
 s = null;
     
   System.gc();
        
Thread.sleep(1000);
      
  System.out.println(s2.get());


  
  }

}

Open in new window

Here i tried to create a weakReference to the same string object..
And then set the strong reference to null.. and ran gc... Waited for garbage collector to run and clear all weakReferences...
But still i see s2.get() returning "test" ?? how is that possible...
Why doesnt garbage collector set the value in s2 as null.
What i understand is since the string "test" now does not have any strong reference to it.. It will be Garbage collected .

Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Your code is wrong. Should be

 
      String s = new String("test");
        WeakReference<String> s2 = new WeakReference<String>(s);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
What is the basic difference between the two...
Your code seems to be working...
Wont s = "test"  and doing s = new String("test")  same string test... ?
Although i understand that
doing new String  will always create a new string whereas using the = operator....will use string pools and wont create a new string if a string is already there...
0
CEHJCommented:
Let's back up a bit. If it's your goal to use/understand WeakReference, i would not use String, as it's a special case. Use Object for now
0
Rohit BajajAuthor Commented:
yes... Got it.. using an Object is clear...
Just for learning sake if you could explain why above String case was not working ?
0
CEHJCommented:
Actually you explained it yourself. With a literal assignment and without the use of explicit creation, the String will be placed in the pool and will remain there.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.