Java - Object declaration out of while loop Could you explain to me which code is better (more optimal) and why?

Vesion 1:

while(something){
Runnable task = (Runnable) taskQueue.dequeue();
Throwable  ex = null;
...
}


Version 2:

Runnable task;
Throwable ex;

while(something){
task = (Runnable) taskQueue.dequeue();
ex = null;
...
}


For me it looks like second version is optimized because variable declaration is out of while loop.
At the bytecode level, there is no such thing as inner block. All local variables are declared at the scope of the method. As a result, both your variants should produce equivalent bytecode. To make sure, decompile them with javap and look at the bytecode. From the programmer's point of view, version 1 is more clear and concise.