What is it better ?

Hi All,

I would like to know your option...

What is the better approach between (a) and (b) ?

a)

MyClass t =null;

for (int i;......){

t =new MyClass();

}

b)

for (int i;......){

MyClass t =new MyClass();

}

Cheers,

ste

[642 byte] By [nichelea] at [2007-11-27 6:13:39]
# 1
Neither is better per se. It would depend on whether you want access to MyClass t after the loop breaks.
orbacha at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 2
In general, it's best to minimize scope where possible. Avoids cluttering your namespace and simplifies debugging. Thus, 99% of the time, Option B is preferred.
hwaitea at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 3
c) for (int i = 0, MyClass t = new MyClass(); ... ) {...} ^_^
uncle_alicea at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 4
b
prob.not.sola at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks to all for your comments...

What about memory usage or garbage collector ?

In my opinion B (or c) is better from the garbage collector point of view, not sure about memory usage.

Of course, the most expensive operation is new MyClass(), but in B the jvm should create any time a new variable (i mean a new pointer container); in A just the same pointer changes value (pointing to the new object)

What do you think ?

Regards,

ste

nichelea at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 6

> Thanks to all for your comments...

>

> What about memory usage or garbage collector ?

>

> In my opinion B (or c) is better from the garbage

> collector point of view, not sure about memory

> usage.

> Of course, the most expensive operation is new

> MyClass(), but in B the jvm should create any time a

> new variable (i mean a new pointer container); in A

> just the same pointer changes value (pointing to the

> new object)

>

> What do you think ?

Doesn't matter. They are about the same.

Kaj

kajbja at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 7

Both versions create the same number of MyClass objects (one per loop iteration).

Both versions have one local variable. There's no "creation" of variables going on.

The only difference between the two is that in (b) the local variable still contains a reference to a MyClass object after the loop ends. When the method containing the code ends, there would be no difference at all between the two versions.

DrClapa at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 8
> In my opinion B (or c) is better from the garbage> collector point of view, not sure about memory> usage.Do you feel you have enough in-depth knowledge about the garbage collection and Java's memory handling to have a valid opinion about that?
-Kayaman-a at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 9

> What about memory usage or garbage collector ?

>

hardly a difference, if any.

> In my opinion B (or c) is better from the garbage

> collector point of view, not sure about memory

> usage.

it isn't. At most, and only if we're talking about a method that runs beyond the next garbage collection cycle, a single instance might take one garbage collection cycle longer to become eligable for garbage collection and be collected in the first scenario.

Unless you're talking about objects taking massive amounts of memory per instance that's peanuts.

> Of course, the most expensive operation is new

> MyClass(), but in B the jvm should create any time a

> new variable (i mean a new pointer container); in A

> just the same pointer changes value (pointing to the

> new object)

>

If you're so worried about those few theoretical microseconds you might save why not declare every variable as a member of the class so you won't have to declare them every time you pass through that method?

Not saying you should (good design practice says you shouldn't) but given your reasoning that would be the logical choice.

But you can't say what'll happen exactly. The compiler or JVM might well decide to optimise away that declaration.

jwentinga at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 10
For Kayaman...no of course...otherwise I had already the answer to my doubt !
nichelea at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 11
For jwenting.Thanks for your answer.I agree with you.Anyway I am not worried about few microseconds but I wanted just to understand better the thing hearing other opinions.CheersSte
nichelea at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 12

> for (int i = 0, MyClass t = new MyClass(); ... ) {

>...

> }

Is that a typo? Because that doesn't even compile ... or am I missing something here?

thomas.behra at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 13

The ellipses (...) represent missing code, and of course, MyClass would need to be defined. Here's a working variant: for (int i = 0, String s = "count"; i < 10; i++)

{

System.out.println(s + i);

}

My point was that the initialization and update portions of the for-loop header can consist of comma-separated lists of expressions.

uncle_alicea at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 14

That's a bridge too far, uncle_a. You can write:

for(int i=0,j=10;...

but not

for(int i=0, String s = ""; ...

The compiler pukes at the second type.

Hippolytea at 2007-7-12 17:22:22 > top of Java-index,Java Essentials,Java Programming...
# 15

> The ellipses (...) represent missing code, and of

> course, MyClass would need to be defined. Here's a

> working variant:

for (int i = 0, String s = "count"; i < 10; i++)

{

System.out.println(s + i);

My point was that the initialization

> and update portions of the for-loop

> header can consist of comma-separated lists of

> expressions.

I could be wrong but I thought a semicolon was needed and not a comma. Sorry Uncle_alice I couldn't help myself ;-P

Aknibbsa at 2007-7-21 21:46:43 > top of Java-index,Java Essentials,Java Programming...
# 16

> I could be wrong but I thought a semicolon was needed and

> not a comma. Sorry Uncle_alice I couldn't help myself ;-P

Yes and no. It turns out the initialization part of the header is one variable initialization expression; you can declare more than one variable in it, but they have to be the same type. So my earlier examples wouldn't work, but this does: for (int i = 0, s = 2; i < 10; i++)

{

System.out.println(s + i);

}

One of these days I'll learn to test all code before I post it. :-/

uncle_alicea at 2007-7-21 21:46:43 > top of Java-index,Java Essentials,Java Programming...