Actually the first one will work. The second one has two problems. One is
NULLPOINTEXCEPTION. The other is you only instantiate one object. The
result is the object in the vector will be referenced to this only one
object, i.e. every advertisement has the same id&text which is the last
record in rs.
Thanks,
Qingyi
>From: Sushil Singh <[EMAIL PROTECTED]>
>Reply-To: "A mailing list for discussion about Sun Microsystem's Java
> Servlet API Technology." <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: Basic Object Reference Question
>Date: Wed, 13 Jun 2001 15:38:38 -0700
>
>Hi All:
>
>Thanks for all your comments.
>
>Another better example is:
>First Senario
>=========
> Advertisement advertisement = null;
> Vector advVector = new Vector();
> while (rs.next())
> {
> advertisement = new Advertisement();
> advertisement.setAdvertisementID (id);
> advertisement.setAdvertisementText(rs.getString(AD_TEXT));
> advVector.add(advertisement);
> }
>Secound Senario
>=========
> Advertisement advertisement = new Advertisement();
> Vector advVector = new Vector();
> while (rs.next())
> {
> advertisement = null;
> advertisement.setAdvertisementID (id);
> advertisement.setAdvertisementText(rs.getString(AD_TEXT));
> advVector.add(advertisement);
> }
>
>Which one is better? In first case, I am creating instance of
>Advertistemnet
>object in a loop, but in secound example its outside the loop.
>
>Awaiting feedback.
>
>Thanks
>
>Sushil
>
>
>Jason Kilgrow wrote:
>
> > It depends on whether or not you are planning to use catID for something
>else.
> > If your concern is that a new string object will be made in the while
>loop,
> > don't worry about it. That's going to happen either way. I think the
>issue is
> > going to come down to reusing catID. If you aren't going to reuse it
>(you
> > aren't in your examples), then I say go with the first snippet. Because
>whether
> > you are going to make a new string object either way.
> >
> > --- Sushil Singh <[EMAIL PROTECTED]> wrote:
> > > Hi,
> > >
> > > 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!!!
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Get personalized email addresses from Yahoo! Mail - only $35
> > a year! http://personal.mail.yahoo.com/
> >
> >
>___________________________________________________________________________
> > 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
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com
___________________________________________________________________________
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