× 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.



JVMs will cache immutable objects.  Even though you've called

new Integer(0)

five times you've only created one Integer object which gets referenced five
times.  You can confirm this by comparing Total1 = Total2.  After being
initialized they should be equal (pointing to the same Integer(0) object).

If you declare variables in the loop body they are scoped to the loop and
can't be used outside the loop.  You might have some end of loop processing
outside the loop which needs the variables.  That would force you to declare
them outside the loop.  Otherwise it doesn't really matter if you declare
your variables in or out of the loop.
------------------
There is no need to assign an object to a variable when you first create it.
You could just as easily have declared

Integer total1; (references null object)

somewhere in the code then use

total1 = methodTotal1();

in your loop body.

You don't have to assign an object to total1 before using it.
methodTotal1() returns an Integer object reference which will get assigned
to total1.
---------------------
Why do you use Integer objects?  Just for an object creation example?  If
you don't use an object in the first place you don't have to worry about
performance with object creation.  Why couldn't you define:

int total1;

then later use

total1 = methodTotal1().intValue(); (assuming methodTotal1 returns an
Integer object)

Better yet if you have control over methodTotal1 modify it to return int
instead of Integer.
---------------------
Take a look at the Java tutorial for a good review of object creation and
usage
(http://java.sun.com/docs/books/tutorial/java/data/objectcreation.html)

Paul


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
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.