In Java if i use String str = new String("test);
10 times, will it create an object 10 times in heap memory?
Or a reference is returned every time? Till now i have studied that new always creates a new object.
In Java if i use String str = new String("test);
10 times, will it create an object 10 times in heap memory?
Or a reference is returned every time? Till now i have studied that new always creates a new object.
It will create 10 objects in memory.
Each time you call new
operator, it gives you a pointer to object in heap. When you let go of that reference, it's garbage-collected.