Consider two lines from code:
...
rs.next(); //rs is ResultSet interface.
rs.getString("name");
...
Byte code generated by javap is :
...
35: invokeinterface #9, 1; //InterfaceMethod java/sql/ResultSet.next:()Z
40: pop
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
51: aload_2
52: invokeinterface #12, 1; //InterfaceMethod java/sql/Connection.close:()V
...
I am trying to interpret this bytecode.
For line rs.getString("name"); bytecode starts from line 43 to 51.
1)At line 45 method is INVOKED and a string object is returned as represented by Ljava/lang/String.Is this right OR here, only method is being loaded from constant pool with index #11 TO STACK and executed at line 50 ???
2) where does the string object returned by rs.getString("name"); lies? on stack or on heap as i think aload_2 is pushing some value on stack.
Actually here, i am confused in the following:
If suppose i have :
...
rs.next();
rs.getString("name");
rs.getString("name");
rs.getString("name");
rs.getString("name");
...10 more times...
...
and all return a same name. Then there will be 10 different string objects with same value.So it will be memory wasting and is a case for using intern(). But if these will be on stack then will it still be considered as memory wastage and need intern()???
3) I think string object returned by rs.getString("name"); is not an interned string so definitely it will not be on Permanent Generation memory area??
The relevant part of the bytecode for the call
is
Taking this line one opcode at a time:
pushes the object stored in local variable #4 (
rs) onto the stackpushes the string constant
"name"onto the stackpops the top 2 items off the stack to call the interface method
java/sql/ResultSet.getString:(Ljava/lang/String;)Ljava/lang/String;and pushes the result (aLjava/lang/String;) onto the stack.then pops the result from the previous method off of the stack.