Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Core java basic String s=new String("ABCD"); s="XYZ"; when s will be garbage collected ?

user-image
Question added by mohammed ghufran , Senior SoftwareEngineer , DP World Asia Holding Ltd
Date Posted: 2018/03/25
Prajakta Naikare
by Prajakta Naikare , Senior Software Developer , Redknee India Pvt Ltd.

There are two conditions under which S will be garbage collected.

1. If S is assigned to null value during the course of application lifetime and after that S is never being reassigned to any other value.

2. When the application terminates JVM shutdown then S will be garbage collected if S is never being assigned to null value. 

PUSKAR KUMAR
by PUSKAR KUMAR , Software Applications Developer , InetSolv technology

now s pointing to "xyz" object so run time "ABCD" removed by garbage collector

Deleted user
by Deleted user

When the string "XYZ" will be assigned to s. the old s string object will be unreachable which means it is a garbage collected.

yahia dekar
by yahia dekar , it engineer , university of sidi bel abbes

s he terminated when the program  he not need him  it s dynamically by jvm

amit kumar  pandey
by amit kumar pandey , PHP Developer , technobrix

when assign s="XYZ";

then reference of creating object of string by using 'new' keyword will be garbage collected 

Ashkan Keshavarzi
by Ashkan Keshavarzi , Senior Java developer , PeykAsa

When the scope of this variable definition is reach to end

Radhwane Fellahi
by Radhwane Fellahi , Software Engineer , ENAGEO SONATRACH

When there are no more references to that object or when the variable goes out of scope.

Partha Sarathi Ghosh
by Partha Sarathi Ghosh , Developer

When the scope of the String s ends, or at the end of the program.

syed faheem uddin
by syed faheem uddin , plant supervisor , Africa snacks

garbage collected will be "ABCD".

More Questions Like This