I don't think that any of the responses that you've received are correct.
RVASUDEV wrote:
>
> In a code fragment like this (the JDBC stuff is not relevant, just used as
> an example):
>
> // load driver, connect to dbURL, create Statement, execute query.
> ResultSet rs;
> while (rs.next()) {
> String col = rs.getString(1);
> // do something with col
> }
>
> My question : how is the declaration/definition/allocation of the col String
> object handled ?
> Is it like this :
>
> a) col is declared/defined/instantiated only on the first iteration
> of the loop,
> and the same object is used on all subsequent iterations
>
> or
>
> b) a new col Java object is declared/defined/instantiated on each
> iteration and
> goes out of scope and is a candidate for garbage collection) at the
> closing
> brace of the while loop ?
>
Keith R Gilbertson wrote:
>
> If you were to use a StringBuffer instead of a string, javac is smart
> enough to optimize so that the object is created outside of the loop.
> However, since you have chosen to use a String here, and Strings are
> immutable, a new String object must be created (without your interference)
> every time you assign a new value to the String. This will happen
> regardless of whether the String col variable was declared inside or
> outside of the loop.
The error here is to confuse the variable col with the object that col refers
to. Based on the code example, javac is not "smart enough to optimize so that
the object is created outside of the loop." The _reference_ may be declared
inside or outside of the loop and it is possible that javac might move the
declaration of col outside the loop. If you declare the reference inside the
loop, you are telling the compiler that the reference is valid ONLY inside the
block created by the if statement. If the compiler moves the declaration outside
the loop, the compiler must not allow any code outside the loop to use the
reference.
The String objects, if any are created at all, are created inside the loop.
Whether or not new String objects are created depends upon the class that
implements ResultSet which is an interface. When you call rs.getString, it is
entirely possible that the String already exists and that the method returns a
reference to an already existing String. It is also possible that the String
already exists and that the ResultSet object creates a copy and passes a
reference to the new String. Or, perhaps the data exists in some other form and
the ResultSet creates a new String and passes a reference to the new String.
> In theory each String is eligible for garbage collection immediately after
> all handles to the object have been lost; here, when the assignment
> statement is performed. But the objects aren't guaranteed to be garbage
> collected at all. This is something to consider, depending on how many
> instances of your servlet will run concurrently.
Only if ResultSet created a new String. Then that new String is eligible for gc
when col is assigned a reference to a different String or when the loop
completes and execution passes outside of the block. If ResultSet returns a
reference to an already existing String, then presumably ResultSet holds a
reference to the String, so reassigning col does not eliminate all references
and the String is not eligible for gc.
Ernie V wrote:
>
> The answer is (b), although it is not always clear with String objects whether a
> new instance will be created on each itteration. And there is no gaurantee that
> the allocated memory will be garbage collected. I would use StringBuffer in this
> case and declare it outside the loop.
Well, this answer is closer to what I consider to be correct, but for the wrong
reason. As I stated above, it does not depend upon the nature of String but upon
the class that implements ResultSet.
Using a StringBuffer may or may not be effective.
This would create a new StringBuffer object every time and assign the reference
to col:
StringBuffer col;
while (rs.next()) {
col = new StringBuffer (rs.getString(1));
}
This would reuse the same StringBuffer object every time
StringBuffer col;
while (rs.next ()) {
col = StringBuffer.append (rs.getString (1));
//do something with col
col.setLength (0) //clears the contents of the StringBuffer
}
Colin Watford wrote:
>
> rs.getString(i) creates the first string.
> The value of col is copied into col.
>
> Each time the loops go round, then the last col and getString are
> flagged for GC - no refernces left. New Strings are allocated space
> for the new connections.
Again, whether rs.getString creates a new String or not depends upon the
implementation of the class. The value of col is not copied into col because col
is not an object, it is a reference to an object. The method rs.getString
returns a reference to a String object; the reference is assigned to the
variable col.
The variable col cannot be flagged for gc because it is not an object, it is a
variable. If a new reference is assigned to col, then the String that col
previously referred to may be marked for gc if there are no other reference to
the String. If col was declared inside the block, when execution passes outside
the block col is out of scope and the last String that col referred to is now
eligible for gc if there are no other references to the String.
Finally, in answer to your question, I believe the answer to be c, some other
way.
The variable col is declared inside the loop. The compiler will create the
variable only once. Where the compiler creates the variable depends upon the
compiler. The same variable is used on all iterations of the loop. Each time
through the loop, depending upon the class that implements ResultSet, a new
String object may or may not be created. A reference to each String object is
assigned to the variable col. If the class that implements ResultSet simply
passes a reference to an existing String object, then no garbage collection
occurs until there are no references to the the ResultSet object. If the
ResultSet object creates a new String every time, then each String is eligible
for garbage collection as a new String reference is assigned to col, or when
execution passes out of the while block (assuming no other references to the
String are created).
Kevin Mukhar
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html