What is difference between prefix and postfix java

What is difference between prefix and postfix java?

or
What is difference between prefix and postfix in forms of the ++i and i++ operator ?

Answer :

  • Prefix: It first increment the value and print it
  • Postfix: If first print value then increment it.

Please see example below:

package com.javahonk.operatortest;

public class PrefixPostfixTest {

    public static void main(String[] args) {

	int j = 10;
	System.out.println("Value incremented: " + (++j));

	j = 15;
	System.out.println("Value before increment: " + j++);

	System.out.println("Value after increment: " + j);

    }

}

 

Output:
What is difference between prefix and postfix java

Leave a Reply

Your email address will not be published. Required fields are marked *