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
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
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
> 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
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.
> 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.
> for (int i = 0, MyClass t = new MyClass(); ... ) {
>...
> }
Is that a typo? Because that doesn't even compile ... or am I missing something here?
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.
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.
> 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
> 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. :-/