Java is one of those languages that you’ve probably met at some point or another. It’s everywhere.

From large enterprise applications like Salesforce to introductory programming classes at university to Android apps on your phone, Java is like your reliable friend in high school who was voted “Most Likely to Succeed” and actually did.

As the years have gone on, Java’s evolved quite a bit. It’s changed its way of thinking, found new ways of communicating and updated its style.

If Java was at your high school reunion, you’d still be able to recognize it – even though it’s gained a few constructs here and there.

Here are some updates that will help you remember who your old pal is:

For-each loop

Java has always had the C-style syntax of for loops:

More often than not, this loop is made to loop through an entire array:

In the original for style loop, the i is initialized to 0 because an array’s first element is 0. The i is smaller than the total length of the array. (Note: it’s using less than [<], not less than or equal to [<=], so it actually stops at 2.) Finally, we increment i for the next bit through the loop. Clear as your memory of high school geometry theorems, right?

Because that style was so common, a new construct was introduced, the for-each loop:

When you encounter the colon, you should read that as “in.” So it’s essentially “for each item in fruit.” Cleaner right? If you didn’t know what that colon was, it might be a little confusing. Any object that can be iterated can be on the right side of that expression. (If you want to give this a spin, in my Java Objects course we loop over the letters in a word during a game of Hangman. Java Objects uses Workspaces, so you don’t need to install anything special on your machine.)

Try With Resources

Oftentimes, you’ll use an object that must be closed after you open it. This type of object is called a resource. A best practice that emerged over time was to use a try and finally block, where you close any resource that had been opened.

That little null check that happens is often forgotten. If it happens that it’s null, you end up with the ever so annoying NullPointerException.

Some objects also can’t be closed twice, so you’d also have to remember to check if it has been closed prior. It was a lot to remember.

So again a new construct was introduced, try with resources:

Any resource declared in that first set of parenthesis will be automatically closed. Anything that wants to use this must implement the AutoCloseable interface. Much better, right? (If you’d like to give this a try, in my Java Data Structures course you’ll have the chance to open up a file of tweets to explore the Java Collections Framework. Java Data Structures also uses Workspaces.)

Diamond Operator

The diamond operator is really a way to avoid repeating generic type declarations. But if you didn’t know about it, it probably would look a little weird. The following two statements are synonymous. Long form:

Shorter form:
Because the declaration defines the parameterized types, the initialization doesn’t need to repeat the types. The compiler can figure it out. This helps trim off some extra line weight.

Lambdas

Speaking of slimming down, Lambdas are anonymous functions that greatly reduce the noise that’s used to surround inline anonymous classes. (I made a really quick 16-minute workshop about Lambdas that you should check out. Only eight minutes if you don’t mind chipmunk speed.) Here’s an example of an event handler in the anonymous inline class version:

Here’s the newer Lambda syntax:

You’d hardly recognize the new version. Aged well, hasn’t it?

Hopefully, this post reacquainted you with your old friend Java. If you want to spend more time catching up, check out my Learn Java track for some hands-on fun. No need to wait another decade.