Sushil Singh wrote:
> I am having a basic object reference question. Consider the following
> code snippet:
> while (rootRs.next())
> {
> categories.addElement(getCategory(rootRs.getString("cat_id")));
> }
>
> Another code snippet:
> String catID = "";
> while (rootRs.next())
> {
> catID = rootRs.getString("cat_id");
> categories.addElement(getCategory(catID));
> }
>
> Now my question is which one is better? When we say
> categories.addElement(getCategory(rootRs.getString("cat_id")));
> then we are getting a reference to the resultset string object or its
> new object. If its a new String object, then the while loop will be
> creating n no of objects throughout the loop!!!
Your rootRs.getString() creates new String instance every time it is
called. You are right that the loop will create N of String instances,
which will be then collected by the garbage collector because in the
next loop run there is no reference to the String instance from the
previous run. We can say it's a little ineffective but there is no way
to avoid this (AFAIK).
Next, I recommend to use the second way of syntax - may be it is a
little slower than the first, but it is much more readable and
understandable (hope this word exists in English :). Remember that you
write the code once, but it is read by others (and probably by you -
after half a year) many times.
Regards,
J.Ch.
--
Ing. Jozef Chocholacek Qbizm Technologies, Inc.
Chief Project Analyst ... the art of internet.
________________________________________________________________
Kralovopolska 139 tel: +420 5 4124 2414
601 12 Brno, CZ http://www.qbizm.com fax: +420 5 4121 2696
___________________________________________________________________________
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