• 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
    • Join Us (JBC)
  • 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
    • Join Us (JBC)

How to Apply the Increment ++ and Decrement — Operators as Prefix and Postfix in Java

July 28, 2023 //  by Talha Malik

The increment ++ and decrement — operators in Java play a key role in incrementing the defined or user input values step-wise. Moreover, these operators are of great aid while performing the iteration of values in a certain range via the “for” loop. These operators can be utilized as “prefix” or “postfix” depending upon the user’s requirement.

This guide will discuss applying the increment ++ and decrement — operators in the form of prefixes and postfixes.

How to Apply the Increment ++ Operator as Prefix and Postfix?

The increment operator ++ used as a prefix such as “++var” increments its value by 1 and is returned. However, in the case of the increment operator “++” as a postfix i.e., “var++”, the original/default value of var is retrieved first and then incremented by 1.

Example 1: Applying the Increment ++ Operator as Prefix and Postfix

This example applies the increment ++ operator as a prefix and postfix:

public class IncrDecr {
  public static void main(String args[]) {
    int firstValue = 3;
    int secondValue = 3;
    System.out.println("Increment Operator as Postfix -> " + firstValue++);
    System.out.println("Increment Operator as Prefix -> " + ++secondValue);
  }
}

In this code, initialize the stated integers. After that, associate the increment ++ operator as a postfix with the former value and then as a prefix with the latter value.

Output

This output implies that the discussed operator applied as a postfix retrieved the original value whereas the operator applied as prefix increments the value.

Example 2: Applying the Increment ++ Operator as Prefix and Postfix With “for” Loop

In this specific example, the discussed operator will be utilized as a prefix and postfix with the “for” loop:

public class IncrDecr {
  public static void main(String args[]) {
    int firstValue = 3;
    int secondValue = 3;
    System.out.println("Increment Operator as Postfix -> ");
    for (int x = 0; x <= firstValue; x++)
      System.out.println(x);
    System.out.println("Increment Operator as Prefix -> ");
    for (int x = 0; x <= secondValue; ++x)
      System.out.println(x);
  }
}

According to these code lines:

  • Likewise, initialize the stated integers.
  • Now, apply the “for” loop to iterate along the integers from 0 till the former defined value and return the iterated values based on the applied operator as postfix i.e., x++.
  • Similarly, repeat this procedure with the latter define value and apply the increment operator as prefix (++x).

Output

As observed, the same outcome is retrieved in both cases since the values are iterated in a loop in a given range.

How to Apply the Decrement — Operator as Prefix and Postfix?

The decrement operator — used as a prefix such as “–var” decrements its value by 1 and is retrieved. In the scenario of the decrement operator “–” as a postfix used as “var–”, the original/default value of var is retrieved first; then its value is decremented by 1.

Example 1: Applying the Decrement — Operator as Prefix and Postfix

This demonstration utilizes the decrement — operator as prefix and postfix:

public class IncrDecr {
  public static void main(String args[]) {
    int firstValue = 3;
    int secondValue = 3;
    System.out.println("Decrement Operator as Postfix -> " + firstValue--);
    System.out.println("Decrement Operator as Prefix -> " + --secondValue);
  }
}

According to this snippet of code, define the provided integer values. Now, apply the decrement — operator as postfix with the first value and prefix with the second value.

Output

This outcome verifies that the decrement — operator applied as postfix returns the default/defined value with no change whereas the operator applied as prefix increments the value on a priority basis.

Example 2: Applying the Decrement — Operator as Prefix and Postfix With “for” Loop

This code example utilizes the decrement — operator to be implemented as prefix and postfix with “for” loop:

public class IncrDecr {
  public static void main(String args[]) {
    int firstValue = 3;
    int secondValue = 3;
    System.out.println("Decrement Operator as Postfix -> ");
    for (int x = firstValue; x >= 0; x--)
      System.out.println(x);
    System.out.println("Decrement Operator as Prefix -> ");
    for (int x = secondValue; x >= 0; --x)
      System.out.println(x);
  }
}

According to this block of code, apply the below-given steps:

  • Similarly, define the values to be evaluated.
  • Now, apply the first “for” loop to apply the decrement — operator as postfix starting from the defined value i.e., 3 till 0.
  • Repeat the same with the latter defined value using a second “for” loop by applying the operator as a prefix.

Output

Here, it can be indicated that the iteration is done identically in both the cases.

Conclusion

The increment ++ and decrement — operators used as prefixes such as “++var” increment and decrement the value by 1 and return it. However, in the case of the operators being used as postfixes like “var++”, the original value is retrieved first; then its value is incremented by 1.

Category: Java

Previous Post: « What is the Java String charAt() Method
Next Post: What Does != Mean in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

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