The Behavior of i=i++ in Java

The Behavior of `i = i++` in Java Featured Image

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.

Table of Contents

Java Loops

As someone who has written a bit of Java learning material, 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:

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:

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. 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:

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:

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:

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

Whether its fighting with the Scanner API or discovering new ways to play Rock Paper Scissors, 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 subscribingOpens in a new tab.. 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.

Coding Tangents (43 Articles)—Series Navigation

As a lifelong learner and aspiring teacher, I find that not all subjects carry the same weight. As a result, some topics can fall through the cracks due to time constraints or other commitments. Personally, I find these lost artifacts to be quite fun to discuss. That’s why I’ve decided to launch a whole series to do just that. Welcome to Coding Tangents, a collection of articles that tackle the edge case topics of software development.

In this series, I’ll be tackling topics that I feel many of my own students have been curious about but never really got the chance to explore. In many cases, these are subjects that I think deserve more exposure in the classroom. For instance, did you ever receive a formal explanation of access modifiers? How about package management? Version control?

In some cases, students are forced to learn these subjects on their own. Naturally, this forms a breeding ground for misconceptions which are made popular in online forums like Stack Overflow and Reddit. With this series, I’m hoping to get back to the basics where these subjects can be tackled in their entirety.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife and kid, playing Overwatch 2, Lethal Company, and Baldur's Gate 3, reading manga, watching Penguins hockey, and traveling the world.

Recent Posts