Reusing an object (by reassigning a value) or creating a new one

B

Berco

What's better (if there is any difference)?
____________
Integer tmp = new Integer(0);
while (true) {
tmp = someRandomNumber;
//store tmp in an array
}

____________

or
____________
while (true) {
Integer i = someRandomNumber;
//store tmp in an array
}
____________


I guess my question is whether it is better to initialize an object once and
reuse it by reassigning a new value to it, or to construct a completely new
object each time?

Cheers,
Berco
 
C

Christophe Vanfleteren

Berco said:
What's better (if there is any difference)?
____________
Integer tmp = new Integer(0);
while (true) {
tmp = someRandomNumber;
//store tmp in an array
}

____________

or
____________
while (true) {
Integer i = someRandomNumber;
//store tmp in an array
}
____________


I guess my question is whether it is better to initialize an object once
and reuse it by reassigning a new value to it, or to construct a
completely new object each time?

Cheers,
Berco

You don't "reuse" the object, you're reusing the reference with the name
tmp. Both constructs will create (and initialize) the same number of
Integer objects (although the first one creates one unnecessary Object
before the loop).
 
J

Joona I Palaste

Berco said:
What's better (if there is any difference)?
____________
Integer tmp = new Integer(0);
while (true) {
tmp = someRandomNumber;
//store tmp in an array
}

or
____________
while (true) {
Integer i = someRandomNumber;
//store tmp in an array
}
____________

I guess my question is whether it is better to initialize an object once and
reuse it by reassigning a new value to it, or to construct a completely new
object each time?

*BOTH* of these approaches construct a completely new object each time.
It's the only way to go if you're using java.lang.Integer. Integer is
immutable and can't be assigned a new value.
What you are doing is reusing a *variable*, which is a different beast
from an object, no matter what C++ programmers might think. =)
The only real issue here is whether you declare the variable outside
your loop or inside it. It matters exactly diddly-squat to the JVM at
run-time - it's the same variable anyway. It does matter to the
compiler, though - with the latter approach you can't use the variable
i outside your loop.
If you don't *intend* to use the variable outside your loop (like one
usually doesn't), I prefer the latter way. It saves clutter in the
source code and makes it easier to read.
 
J

Joseph Dionne

Berco said:
What's better (if there is any difference)?
____________
Integer tmp = new Integer(0);
while (true) {
tmp = someRandomNumber;
//store tmp in an array
}

____________

or
____________
while (true) {
Integer i = someRandomNumber;
//store tmp in an array
}
____________


I guess my question is whether it is better to initialize an object once and
reuse it by reassigning a new value to it, or to construct a completely new
object each time?

Cheers,
Berco

I assume someRandomNumber is an int primitive. Since an Object is
immutable, you need to code tmp = new Integer(someRandomNumber). Your
variable tmp is not a int primitive, but rather an Object.
 
C

Christophe Vanfleteren

Frank said:
Hi!



?? Are all objects in Java supposed to be mmutable?

Frank

He probably meant that Integer is immutable.
Wether an instance is supposed to be mutable or not is up to the designer of
the class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top