Java String & Garbage Collection Explained

Understanding Java String & Garbage Collection 🧠☕ String objects created using String literals are stored in the String Constant Pool and are not eligible for Garbage Collection, even after setting the reference to null. This example clearly shows why the same hashCode() is returned and how Java optimizes memory using immutability and pooling. 📌 Key concepts: ✔ String Pool ✔ Immutability ✔ Garbage Collection ✔ hashCode() Java String + GC explained with a simple example 🚀 Even after calling System.gc(), String literals are not removed because they live in the String Pool. Same content ➝ same hashCode() ➝ same object reference. Learning Java step by step ☕📘 #Java #CoreJava #JavaInterview #GarbageCollection #StringPool #LearningJava #OOPs

  • text

To view or add a comment, sign in

Explore content categories