Java ++ Incrementor: Java Increment Shorthand & How To Use It

Athena Ozanich
Athena Ozanich



Shorthand is one of the most valuable tools available to writers, but it doesn’t end there because programmers also have access to shorthand coding.

A young woman diligently studying the benefits of Java shorthand increment ++ and decrement -- operations.

In this post, you will discover the benefits of using shorthand programming syntax to simplify your coding experience. Specifically, you will learn about Java's shorthand for incrementing and decrementing. You will learn how it works and see some coding examples of how to perform this task.

Without any further delay, let’s jump into it.

Download Now: An Introduction to Java & JavaScript

Java Shorthand Syntax

Like many languages, Java offers many shorthand syntax options to help simplify the development process. The operator shorthand facilitates performing logic and arithmetic operations. Several types of shorthand syntaxes are available for completing a number of operations. We won’t cover all of them in this post, but we will discuss the shorthand known as the incrementor and decrementer operators. Let’s start with the incrementor, taking a more in-depth look at the operator and how it works.

Java ++ Increment Operator

You will often find yourself needing to increase a value in a logical way, such as when using loops or functions. As such, there are a number of ways you can accomplish this, but the initial way is lengthy and leads to repeating code way too much.

To alleviate this problem, developers came up with a shorthand syntax to help support the “DRY” (Don’t Repeat Yourself) principle. Let’s start this exploratory process by looking at the standard approach for incrementing a number value stored in a variable.

This next image showcases the shorthand syntax for incrementing and decrementing.

Java Increment Process Example

The standard way to increment int value in Java is unnecessarily long and typing the same thing repeatedly is just annoying. Below is an example of the standard approach for incrementing a value.

int num = 1; num = num + 1;

Could you imagine being stuck writing that over and over? It doesn’t seem like much until you start to consider large and robust software applications where this would be needed many times. This process would get very old and create a lot of unnecessary surface area in our code. The extra code makes the software hard to read for developers and leaves more room for potential bugs.

Now let's look at the shorthand syntax for this process.

Java Increment Shorthand Example

To simplify this process, Java gives us the ++ operator, which is far shorter and makes the code a little easier to read. The benefits are best seen when used in practice. Let’s compare the difference between the two concepts.

Instead of writing out all of the above, you can write out the following line of code and achieve the same results with better reliability.


Yeah, that’s all there is to it. The ++ operator tells Java that what we want is to take the current value and add one to it. But even more remarkable than that, you don’t need to place the ++ operator after the variable; you can also put it in front. By placing the operator first, Java assumes a number and adds one to it before reading the value.

Ensure you only use these operators with compatible values; otherwise, it can cause erroneous behavior.

Java -- Decrement Operator

Big surprise here, the -- operator is the exact opposite, and it behaves similarly, but through a value reduction. Using this operator, you can reduce a value by one, and just like the incrementor operator; you can place it before or after the variable.

Using Java Loops To Increment and Decrement

There are many types of loops, and each type uses some kind of conditional data to control the number of iterations. And very often, they are used with an incrementing variable to help set constraints and dictate certain behaviors. Let's look at an example of a Java for loop that does both.

public class Main { public static void main(String[] args) { //Instantiate current score int score = 0; //Instantiate current number of players int players = 4; //Static WinState String winState = "won"; for(int i = 0; i < players; i++){ String winStateMess = "You "+winState+"!"; switch (winState) { case "won": score++; System.out.println(winStateMess+" Your score is: "+score); break; case "lost": System.out.println(winStateMess); break; case "tied": System.out.println(winStateMess); break; } } } }

This block of code may look daunting, but it’s actually very simple and uses the ++ incrementing in two ways. The loop compares an incrementor variable to the number of players, loops once for each player, and checks for the current winState value. Using a switch statement, it checks the value for one of three cases; if a match is found, the code inside of the case runs, and the switch ends. This process repeats once for each player, which is useful for tracking and updating player scores.

Moving Forward With Java Shorthand Syntax

There are a lot of ways to move forward using Java shorthand syntax; not the least of all is practice. Using this shorthand syntax for incrementing and decrementing, you can create cleaner, more concise code. While using shorthand syntax is often a choice, in most cases, the overall quality of your code will improve by incorporating these tricks into your workflow.


Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about the differences between and uses of these popular programming languages.