Is there any difference?

what’s the difference between

Rectangle search; for (Client c : clients) { search = new Rectangle(); //do the updating }

and

for (Client c : clients) { Rectangle search = new Rectangle(); //do the updating }

been wondering if this does anything performance wise for a long while now

Not that you should be worried about because the compiler will usually attempt to make it as efficient as possible on a micro level.

thanks :slight_smile:

I have found out in some cases that doing that does bring me somekind of issues though.

you’re probably talking about the scope of the variable.
in the first example you declare it before the for-statement so you can use it after the for-statement as well
in the second example you create it inside the for-statement so you can only use it inside the for-statement

By convention, variables should be declared in the smallest scope possible, so the second one is preferred if the variable is not used outside the loop

function a(set)
for i in set return i where i is equal to x
end

v = a({…})

v
for i in {…} v = i where i is equal to x

are equivalents. But what Davidi2 said.

[quote=“Cres, post:5, topic:555083”][quote author=Fabrice L link=topic=674007.msg4506273#msg4506273 date=1459102067]
I have found out in some cases that doing that does bring me somekind of issues though.
[/quote]

you’re probably talking about the scope of the variable.
in the first example you declare it before the for-statement so you can use it after the for-statement as well
in the second example you create it inside the for-statement so you can only use it inside the for-statement[/quote]

I know how that works mate :stuck_out_tongue:

in that case idk what kind of issues you could have gotten

thx david :stuck_out_tongue:

[quote=“Cres, post:9, topic:555083”]in that case idk what kind of issues you could have gotten

thx david :p[/quote]

I’m trying to replicate it, I’ll let you know when I find it

[quote=“Cres, post:1, topic:555083”]what’s the difference between

Rectangle search; for (Client c : clients) { search = new Rectangle(); //do the updating }

and

for (Client c : clients) { Rectangle search = new Rectangle(); //do the updating }

been wondering if this does anything performance wise for a long while now[/quote]

I might be completely wrong here, so please correct me if I am.

  • I do realise the compiler optimises both samples to about the same bytecode

But doesn’t the first statement (Rectangle search;) find free space on the machine, which it keeps occupied for data which will eventually get allocated to it (search = new Rectangle();)?
So the loop would just assign different data to the occupied space on the machine each time?

This whilst the second sample searches for free space on the machine to allocate the data to, each single iteration?

“Free space” for a pointer… in both examples you have 1 active pointer at all times, it isn’t creating space for the object iirc.

No - that isn’t how it works. Local variables are stored in a fixed-sized array in the stack frame, which is allocated upon entering the method and freed upon leaving the method.

Declaring a variable isn’t something that happens at runtime (note that instantiating a variable is separate to declaring a variable, and instantiation does happen at runtime). Declarations of local variables are each dealt with once at compile time by the compiler, when it’s deciding how large the stack frame for each method needs to be. The Java compiler doesn’t care about the variable scope when it’s working this out. Therefore, the bytecode generated by javac is identical. And by extension, the performance will also be identical.

As people have said earlier, using the narrowest scope possible is best practice for readability/maintainability.

Premature optimization is like trying to build furniture without a measuring stick. Following instructions written in Youtube comments.
Focus on readability, measure and then optimize the parts that aren’t running so well.
Majority of the time, the issue isn’t where you think it is and you’ve just wasted effort writing chicken scrawl (there are some exceptions of course).

I wasn’t trying to optimize anything, was just wondering if there was any differency in bytecode/compilation.

Why don’t you try it out? I think Java has a built-in tool for this.

javap -c