Let we have a Student class. Form the following code snippet (Java) we know -
Student aStudent = new Student();
- A 'Student' type reference variable is created
- An object of 'Student' is created with the 'new Student()'
- The object is assigned with the reference variable 'aStudent'
So far I know, each time we write 'new Student()' a new object is created and the newly created object is allocated a memory space. But sometimes we write something like this in a for loop -
for ( int i=0; i<10000; i++) {
Student student = new Student();
...
...
...
}
In this situation -
- does JVM create new object of Student 10000 times? Or any optimization is occurred behind the scene to save memory.
- If any optimization occurred then how it is done? And how can I know the number of actually created object in the for loop.
Thanks in advance.