Immutability of String in JAVA -


consider code :

public class stringer {      public static void main(string[] args) {          string s1 = "someline";         system.out.println(s1);  // prints someline         s1 = "anotherline";         system.out.println(s1);  // prints anotherline     }  } 

when change s1 someline anotherline , since strings immutable , mean someline lost , eligible gc ?

much appreciated

when change s1 someline anotherline, since strings immutable, mean someline lost , eligible gc?

the fact string immutable irrelevant question. fact reassigning variable , therefore losing other reference determines if object candidate gc or not.

to answer question, if wasn't string literal.

moreover, string literal refers same instance of class string. because string literals - or, more generally, strings values of constant expressions (§15.28) - "interned" share unique instances, using method string.intern.

that string object won't garbage collected because classloader , corresponding class have reference it.

related reading:


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -