× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Thanks Paul!  That is what I was looking for and what you said made perfect
sense.

-----Original Message-----
From: java400-l-bounces@xxxxxxxxxxxx [mailto:java400-l-bounces@xxxxxxxxxxxx]
On Behalf Of Clapham, Paul
Sent: Thursday, June 16, 2005 10:16 AM
To: Java Programming on and around the iSeries / AS400
Subject: RE: Cost of instantiating

Variables and objects aren't the same thing. Creating new objects is done by
the "new" keyword and where the variables that refer to those objects are
declared is irrelevant (at least as far as numbers of objects created is
concerned).

It's best to declare variables in the smallest scope possible (i.e. the
second version). If you are going to use the first version, it would be
better to initialize the variables to null. The code you have there creates
five objects that are thrown away almost immediately.

And in this case, since an Integer object is immutable, you could cut down
on the number of objects created like this:

Iterator iter = myList.iterator();
while (iter.hasNext()) {

    Integer total1 = new Integer(0);
    Integer total2 = total1;
    Integer total3 = total1;
    Integer total4 = total1;
    Integer total5 = total1;
    
    total1 = methodTotal1();
    total1 = methodTotal1();
    total1 = methodTotal1();
    total1 = methodTotal1();
    total1 = methodTotal1();    
    // write totals to PDF
}

But again, total1 is assigned the value of methodTotal1(), so whatever value
it had before gets thrown away. Assuming that you didn't finish editing your
posted code, an even better piece of code would be

Iterator iter = myList.iterator();
while (iter.hasNext()) {

    Integer total1 = methodTotal1();
    Integer total2 = methodTotal2();
    Integer total3 = methodTotal3();
    Integer total4 = methodTotal4();
    Integer total5 = methodTotal5();    
    // write totals to PDF
}

in which no "new Integer(0)" objects are created to be immediately thrown
away.

HTH
PC2

-----Original Message-----
From: java400-l-bounces@xxxxxxxxxxxx [mailto:java400-l-bounces@xxxxxxxxxxxx]
On Behalf Of albartell
Sent: June 16, 2005 07:52
To: 'Java Programming on and around the iSeries / AS400'
Subject: Cost of instantiating

I have been reading in different publications by well known authors about
how we don't need to be as concerned about instantiating a few extra objects
here and there because the processing in JDK1.5 is so much better than that
of JDK1.3  (note that these comments have come in from the J2EE crowd).

So I have an example I would like comments on below. In one I create my
objects outside of the while loop and in the latter one I create the
variables within my while loop. Note that I don't need to have access to the
variables outside my while loop, but if I declare them outside the while
loop I need to initialize them with each loop iteration.

My question would be, do I save anything (meaning memory, CPU cycles, etc)
by declaring my variables outside my while loop?


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.