4
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?
2
  • 2
    if you run using the Hotspot VM, it will most probably optimize the int allocation, as well as the object reference away. Commented Aug 7, 2012 at 11:54
  • 1
    Prefix or suffix (++i or i++) only matters when you put them into an assignment (there is no performance difference though). Commented Aug 7, 2012 at 12:00

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

7 Comments

So do you think it is a good style to write the following loop as Object[] objs = new Object[5]; int j; Object obj; for (int i = 0; i < 5; ++i) { j = i + 1; obj = objs[i]; }
No. Not at all. variables should be declared in the narrowest possible scope.
No, as reserving place for them is a cheap operation, and IMO, writing correct and clear code is much more important.
@OneMoreVladimir You won't see a difference in terms of performance.
Remember that declaring a variable is not the same as instantiating an object. Putting the "int j" and "Object obj" declarations outside the loop doesn't save any memory or CPU cycles, and instead, the objects stay in memory longer till the end of the block instead of till the end of the for loop.
|
6

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

Every time you loop, a new int is created but obj is only a reference to an existing object so that does not trigger an object creation. In any case, it is very likely that the JVM will optimise that for you.

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

Most likely nothing noticeable, and once again, the JVM will probably change your code anyway.

Bottom line: use whatever is more readable and limits the scope of the variables to where they are used.

Actually, the JVM will probably change your code to this, because you don't use the local variables in your loop:

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

1 Comment

@onemorevladimir: I was going to comment in the other answer, but this one got to the point: the JVM will optimize your code, and you won't even be able to compare results of puting variables, etc., inside or outside the loop. To test, you need to actually use those variables inside the loop, so that JVM can let them out.
3

I have compared both bytecode of code portions. I have found nothing special. there is only one difference is that in first sample code portion there are 4 local variables defined, but in second sample code portions there are 5 local varibables defined. However similar opcodes are executed. Since local variable names are different difference opcodes are executes. I mean in first example 'i is defined as local-variable-2', in second example 'i is defined as local-variable-4'.

But monitoring code execution via JVM tools can give extra information. Since Java is a platform and JVM may optimize the code execution, we may not be sure by looking at either java source code or java bytecode.

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

 local-variable-0=this
 local-variable-1=objs
 local-variable-2=i
 local-variable-3=j

  stack=2, locals=5, args_size=1 
 0: aload_0 
 1: invokespecial #8                  // Method java/lang/Object."<init>":()V 
 4: iconst_5 
 5: anewarray     #3                  // class java/lang/Object 
 8: astore_1 #store created object referance to local value-1 (objs)
 9: iconst_0 #push 0 on stack
10: istore_2  # local-value-2(i) assigned 0
11: goto          26 
14: iload_2  #load localvalue 2(i)
15: iconst_1 # ++i operation
16: iadd  #i+1
17: istore_3 #assign i+1 into local-variable-3(j)
18: aload_1 # push object in local varable 1 (objs) onto the stack
19: iload_2 # push integer in local variable 2 (i) onto the stack
20: aaload  # retrieve entry
21: astore        4 # push value on stack into local-variable-4 (obj)
23: iinc          2, 1 # local-variable-2(i)++
26: iload_2 
27: iconst_5 
28: if_icmplt     14 for(if i==5)
31: return 

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

  local-variable-0=this
 local-variable-1=objs
 local-variable-2=j
 local-variable-3=obj
 local-variable-4=i

  stack=2, locals=5, args_size=1 
     0: aload_0 
     1: invokespecial #8                  // Method java/lang/Object."<init>":()V 
     4: iconst_5 
     5: anewarray     #3                  // class java/lang/Object 
     8: astore_1 #store created object referance to local value-1 (objs)
     9: iconst_0 #push zero on to the stack
    10: istore        4 # local variable-4(i) is assigned zero
    12: goto          28 
    15: iload         4 
    17: iconst_1 
    18: iadd  #i+1
    19: istore_2  #j is set 
    20: aload_1  #load objs
    21: iload         4 #load i
    23: aaload 
    24: astore_3  #obj=objs[i]
    25: iinc          4, 1 # i++
    28: iload         4 
    30: iconst_5 
    31: if_icmplt     15 # if i==5
    34: return 

1 Comment

I am preparing my comments, I am going to update my answer soon.
1

Yes they are!

Do it like this :

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

1 Comment

Your method actually creates a new Object which his doesn't.
1

Yes they created every time and prefix or suffix ++ doesn't matter really.

Comments

1

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

In theory yes. In practice optimise code this will not happen. However, in your case a loop of 5 (or less than 10,000) might not be optimised as it performance isn't likely to matter.

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

For optimised code, they are the same here. For unoptimised code, the difference is unlikely to matter.

Comments

1

I will try to answer your question....

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

int j is created everytime, and so does the Object Reference Variable obj is also created everytime inside the loop, But NOT THE OBJECT.

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

In this case where there is No assignment of the j's value, it won't matter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.