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.
-ernie
RVASUDEV wrote:
> Hi,
>
> (I posted this on a Java mailing list, got a couple of answers,
> not too sure about their validity. Know that a lot of the people
> on this list are Java gurus, so I'm posting it here).
>
> 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 ?
>
> or
>
> c) some other way ?
>
> TIA
> Vasudev
>
> ___________________________________________________________________________
> 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
___________________________________________________________________________
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