for statement
rs.getString("name") //rs is java.sql.ResultSet interface
bytecode is:
41: aload 4
43: ldc #10; //String name
45: invokeinterface #11, 2; //InterfaceMethod java/sql/ResultSet.getString:(Ljava/lang/String;)Ljava/lang/String;
50: pop
At line 45 returned string object by rs.getString("name") is pushed onto stack and at line 50, return object (a string object) is poped up.
1)Does stack contain only reference to this returned string object with actual object on heap OR stack contains the actual String object??
2) and after poping up the returned string object, will it be garbage collected OR it’s memory deallocated as stack for this method vanishes???
There is no returned string object. There’s a returned string reference.
Conceptually at least, objects are never on the stack, never returned, never passed. It’s only ever a reference.
The reference will be cleared when the stack pops, which means that the string object itself would be eligible for garbage collection if there are no other strong references to it.