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:
- 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 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 subscribing. 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.
Recent Posts
Recently, I was thinking about the old Pavlov's dog story and how we hardly treat our students any different. While reflecting on this idea, I decided to write the whole thing up for others to read....
In the world of programming languages, expressions are an interesting concept that folks tend to implicitly understand but might not be able to define. As a result, I figured I'd take a crack at...