Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java loop questions

Tags:

java

for-loop

Object[] objs = new Object[5];
for (int i = 0; i < 5; ++i) {
    int j = i + 1;
    Object obj = objs[i];
}

I have two questions for the above loop:

  1. Are the j variable and obj reference created for every loop iteration or they are created once and then only reassigned the values?
  2. Is there any perfomance benefit of putting ++i instead of i++ as a single instruction to increment the value?
like image 276
OneMoreVladimir Avatar asked Nov 30 '22 05:11

OneMoreVladimir


1 Answers

Are the j variable and obj reference created for every loop iteration or they are created once and then only reassigned the values?

Declared and created every time

Is there any perfomance benefit of putting ++i instead of i++ as a single instruction to increment the value?

Not really.

like image 57
MByD Avatar answered Dec 02 '22 18:12

MByD