• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

What is the Difference Between ++i and i++ in Java

July 26, 2023 //  by Talha Malik

In Java programming, iteration is a vital functionality to access various elements in an appropriate manner. This can be achieved with the help of the “pre-increment(++i)” and “post-increment(i++)” operators in Java that iterate through the specified elements or increment the defined values and return them individually.

This tutorial will state the core differences between the Java “pre-increment(++i)” and post-increment (i++)” operators in Java.

What is the Difference Between “++i” and “i++” in Java?

The pre-increment operator “++i” is used in the code if there is a need to increment the value by “1” and then utilize it. On the other hand, the post-increment operator “i++” comes into effect when it is required to use the current value, and then increment the value by “1”. However, both operators perform in an identical manner.

Example 1: Applying Both the Pre-Increment(++i) and Post Increment(i++) Operators to Return the Same Outcome

This example applies both the pre and post-increment operators in the “for” loop to return the same outcome. In the first case, the pre-increment operator “++i” is utilized, as follows:

public class Iter {
public static void main(String[] args){
  for (int i = 0; i<=5;++i)
  System.out.println(i);
}}

In the above block of code, apply the “for” loop that iterates along the integers from “0” to “5” by incrementing them first based on the applied pre-increment operator “++i” and returns them.

Output

The output implies that the integers are iterated and returned accordingly.

The following demonstration utilizes the post-increment operator “i++” along with the “for” loop to perform the discussed iteration and retrieve the same output:

Example 2: Applying Both the Pre-Increment (++i) and Post Increment(i++) Operators to Return a Different Outcome 

In this specific example, both the discussed operators can be applied to generate a different outcome. It is such that the post-increment operator(i++) does not affect the constants:

public class Iter {
public static void main(String[] args){
  int x = 1;
  int y = 1;
  System.out.println("Pre Increment -> "+ ++x);
  System.out.println("Post Increment -> " + y++);
}}

In these code lines:

  • Initialize the stated variables that need to be incremented.
  • After that, associate the pre-increment operator “++x” with the former initialized value and the post-increment operator “y++” with the latter defined value.
  • Lastly, observe the outcome in both scenarios.

Output

In this output, it can be verified that the pre-increment operator(++i) increments the value while it is not the case with the post-increment operator(i++) i.e., the value remains the same.

Conclusion

The pre-increment operator “++i” is used if there is a need to increment the value by “1” and then utilize it whereas the post-increment operator “i++” comes into effect when it is required to use the current value, and then increment it by “1”.

Category: Java

Previous Post: « Length vs Length() Method in Java
Next Post: What Does != Mean in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact