The Behavior of `i = i++` in Java
Tags: java, beginners, tutorial, coding
Published: 2019-03-22 14:50:20

---
title: The Behavior of `i = i++` in Java
published: true
tags: Java, Beginners, Tutorial, Coding 
canonical_url: https://therenegadecoder.com/code/the-behavior-of-i-equals-i-plus-plus-in-java/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/ysolkt70gl6jz1fg1aa6.jpeg
description: As I continue to teach, I'm constantly introduced to new ways to code. For instance, one of my students just introduced me to `i = i++`. Care to guess how it works?
series: Coding Tangents
---

A little while back, I was running a Java lab, and one of my students had asked me why their code wasn’t working. As I glanced over their solution, I noticed a peculiar line that read `i = i++`. Up until that point, I had never seen any code like it, and I certainly had never tried it. So, I did what any good teacher would do and took to Google to find out more.

## Java Loops

As someone who has written [a bit of Java learning material][1], I consider myself at the very least proficient in the language. However, as I continue to teach, I find that students always come up with creative ways to stretch the language.

In my recent attempt to teach loops, I stumbled up a student solution that had the following while loop syntax:

```java
int i = 0;
while (i < 10) {
  i = i++;
}
```

Perhaps this odd bit of code was my fault. After all, I often tell my students that there are many ways to solve a problem.

To aid in that message, I like to switch up my syntax every now and then. For instance, it’s not uncommon for me to share solutions to looping problems using a different loop syntax each time. Sometimes I’ll use a for loop while other times I’ll use a while loop. If I’m feeling good, I might even show off a for each loop. Every once in a while I’ll even throw in some recursion to shake things up, but you’ll never catch me dead using a do while loop.

At any rate, this mentality shows up when I increment variables as well. For example, I tend to teach `i = i + 1` first because it trips students up who haven’t recognized that the equal sign is really the assignment operator. Of course, once I think they’ve grasped the concept, I usually resort to shorthand like `i++` and `i += 1`.

Unfortunately, my push for self-expression can sometimes result in mangled syntax like:

```java
int i = 0;
while (i < 10) {
  // Do something
  i++;
} else {
  // Do something else
}
```

Every time I see something like this I feel like I move one step closer to overcoming the [curse of knowledge][2]. Enter: `i = i++`.

## Java Increment Operators

As I mentioned already, there are a lot of ways to increment a variable in Java. The following list covers quite a few examples—albeit some are silly:

```java
i = i + 1;
i += 1;
i++;
++i;
i = i - -1;
i -= -1;
i = -(~i)
```

Despite all the options above, I’d never thought to try `i = i++`, yet it makes a lot of sense. After all, it looks like we increment the variable and store the result. To be honest, I was little perplexed when I saw it live in the lab.

As it turns out, `i = i++` doesn’t work as expected, and it has to do with how the right side of the assignment statement is evaluated. Since `i++` is a postfix operator, i is incremented but its old value is returned. As a result, i overwrites itself with the old value.

If the explanation above isn’t great, try taking a look at the following code snippet which replicates the behavior:

```java
int temp = i;
i++;
i = temp;
```

If we used the `i = i++` syntax in a loop, we’d have an infinite loop on our hands with little at our disposal to diagnose the bug. Thankfully, I was there to at least recognize the issue. Whether or not I actually understood the bug was a separate problem, but I’m sure I saved that student a lot of time. Now, I hope to share my findings with others!

## The Take Home

At this point in the series, I’m starting to notice a trend:

- A student writes some interesting code
- It does/n’t work to my surprise
- I spend some time perplexed
- I write an article to discuss my findings

Whether its [fighting with the Scanner API][3] or [discovering new ways to play Rock Paper Scissors][4], I’ve got to say that I’m having a lot of fun learning from my students. I absolutely cannot wait to become a professor.

In the meantime, I’ll continue to document all the fun I’m having while teaching. If you’re enjoying my work, [consider subscribing][5]. If you’re already subscribed, you can help spread the word by sharing this article with people you know.

At any rate, thanks again for taking the time to support my work.

[1]: https://therenegadecoder.com/series/java-basics
[2]: https://therenegadecoder.com/blog/the-curse-of-knowledge/
[3]: https://therenegadecoder.com/code/be-careful-with-scanner-methods-in-java/
[4]: https://therenegadecoder.com/code/rock-paper-scissors-using-modular-arithmetic/
[5]: https://therenegadecoder.com/members/