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:
Contents [show]
For-each loop
Java has always had the C-style syntax of for loops:
for (initialize; terminate; increment) { | |
//... | |
} |
String[] fruit = {"apple", "cherry", "banana"}; | |
for (int i = 0; i < fruit.length; i++) { | |
String item = fruit[i]; | |
System.out.println("Yum a delicious " + item); | |
} |
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:
String[] fruit = {"apple", "cherry", "banana"}; | |
for (String item : fruit) { | |
System.out.println("Yum a delicious " + item); | |
} |
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.
BufferedReader br = new BufferedReader(new FileReader(path)); | |
try { | |
return br.readLine(); | |
} finally { | |
if (br != null) { | |
br.close(); | |
} | |
} |
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:
try ( | |
BufferedReader br = new BufferedReader(new FileReader(path)); | |
) { | |
return br.readLine(); | |
} |
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:
Map<String, Integer> counts = new HashMap<String, Integer>(); |
Shorter form:
Map<String, Integer> counts = new HashMap<>(); |
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:
btn.setOnAction(new EventHandler() { | |
@Override | |
public void handle(ActionEvent evt) { | |
System.out.println("You should watch the Lambdas workshop!"); | |
} | |
}); |
Here’s the newer Lambda syntax:
btn.setOnAction(evt -> System.out.println("Watch it now!")); |
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.
Great Blog! I am a pending teamtreehouse success story. After working through your Java Track and half of the Android Development Track I was able to write an Android App and am currently in the process of selling it to the company I have been working for, for the past decade. Thanks for putting me on the “Track” to realizing my dream of becoming a Software Developer!!
Hi Mike! That’s so great to hear, congratulations! I’d to chat to you more about your experience. Would you mind emailing me at faye@teamtreehouse.com? Thanks! 🙂