🙋 Need help? Ask an expert now!

The increment operator changes a variable by the value of one. Instead of writing varOne = varOne + 1; you can write varOne++; and it will do the same thing. Let's review an example of incrementing variables.
package exlcode;

public class IncrementExample {

  public static int exampleVariableOne = 10;

  public static void main(String[] args) {
    // Both statements adds 1 to exampleVariableOne

There are two ways to use the increment operator; prefix and postfix increment. The prefix increment looks like ++variablename; while the postfix increment looks like variablename++;. Both of these operations add one to the value in the variable. The difference between the two is the order of how it works. If we use the "++" operator as a prefix like ++varOne;, the value of varOne is incremented by one before the value of varOne is returned. If we use ++ operator as postfix like varOne++;, the original value of varOne is returned before varOne is incremented by one. This means that the original value of varOne is printed while the value increases by one after the print statement. It is essential to know the difference between the two types of incrementing even though we usually don't print these values.

Edit Me on GitHub!

Application Question

Consider the following code segment:

int varOne = 0;
int varTwo = 1;
varOne = varTwo++;
System.out.println("varOne: " + varOne + " varTwo: " + varTwo);

What is printed as a result of executing this code segment?