What's the "performance" difference between i++ and ++i? 2 senarios:1. i++; vs ++i;2. int j = i++; vs int j = ++i;

Showing Answers 1 - 2 of 2 Answers

agyeya

  • Jan 11th, 2007
 

There is definitely no difference between ++i and i++ if they are used as statements on their own. Only the presence of an additional assignmentforces a difference in the compiled bytecode.In the context of an assignment, it is possible that comparing theuse of the pre-increment or post-increment operators could result indifferent runtimes. But that is unlikely where the functional result ofusing either is the same.

  Was this answer useful?  Yes

Y_NOT_

  • Feb 6th, 2007
 

public class Increment{ public Increment() { int i = 0, j = 0; System.out.println( "1- j = " + j ); j = i++; // prints 0 but infact the value of j is 1. System.out.println( "2- j = " + j ); // j will be 2 now. j = i + 1; System.out.println( "3- j = " + j ); } public static void main( String[] args ) { new Increment(); }}

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.