Ever had the experience where you think you speak a language pretty well, and then you stumble upon a word you didn’t know existed? As a back-end designer working out of Navan’s Tel Aviv office, I speak English daily but always find new-to-me words. My latest is ramble. I’ve heard that the best way to cement new words in your mind is to try to use them in a sentence immediately …
So, that’s enough rambling from me! I’m here to share with you 7 Java features that may have slipped under your radar. How many of them can you use in your code by the end of today? Let’s dive in!
What does it do? Streamlines code, reduces demand on memory
When should you use it? When you need improve performance (e.g., your application starts crashing!)
Consider the following well-known example, in which the system fails to see that two sets of characters are identical:
Even though str1 and str2 are carbon copies of one another, the system treats them as unique entities because each of them is stored in a different spot in the memory.
Now, scale this example up and consider a JSON parser implementation — where fields such as name and UserID are repeated over and over, potentially hundreds of thousands of times, on each and every JSON object…
What a waste of memory!
We can get around this by invoking String.intern(), which instructs the Java Virtual Machine to check whether the string is already present in the String Constant Pool. If the string is present, its reference is returned.
What is it? An elegant way to initialize collections
When should you use it? To make collections more readable and easier to maintain
Usually, when we want to build a collection, we first create an empty list and then populate that list, entry by entry. Double-braces let us roll these two steps into a single expression, shaving time off the initial setup and improving the code’s readability for future users:
What does it do? Allows us to direct program control to different areas of the code
When should you use it? To navigate within nested loop statements
In C, C++ and other programming languages, we often use the command goto to transfer program control to a labeled spot in the code. So far, Java doesn’t support goto (although it is a reserved word, in case the developers decide to add it in the future). But there are times — such as before nested loop statements — where it would certainly come in handy!
Thankfully, we can use labeled breaks and continues.
We know that for nested loops, the break statement terminates the innermost loop. Here’s an example of how we can break an outer loop:
Calling firstNumsToSum(List.of(0,1,2), List.of(1,2,3), 3) will print:
This second example shows how we can use label breaks to interrupt a try block (surprising right?!):
If the needToUpdateRecord step returns *true*, the output will be:
And if it returns *false*, we’ll get:
What does it do? Holds multiple small data objects in a single file
When should you use it? To reduce the number of individual files you need to store
If you’re a Java developer, you likely already use nested classes to logically group together elements that will only be used in one place — and produce sleeker code.
But did you know that multiple non-nested classes can be stored in a single Java file, too? For instance, you might choose to store user data such as first name, last name, and phone number within the same file instead of across three separate ones, along the lines of an old-school index card.
In the example below, items in a shopping cart are gathered together in a single file for convenience:
What does it do? Sorts elements based on their delay time
When should you use it? To schedule actions to be executed in the future
The DelayQueue is the unsung hero of the Queue interface implementation. It works by blocking elements internally, preventing them from being taken from the queue and executed until their assigned delay time is up.
In the example below, PasswordExpirationNotification will become accessible in the queue one day before the end user’s password is due to expire.
Note:
What does it do? Makes numbers more readable
When should you use it? Whenever you’re using numbers over 10,000 or so
There’s nothing technical about this one, but your eyes will thank you! Underscores are a neat way to make code that includes large numbers infinitely more readable.
What does it do? Just for fun; makes the system “talk back”
When should you use it? On April Fool’s Day, or when you want to troll your colleagues (!)
Comments are invisible to Java compilers and therefore not executable. But if you’re feeling mischievous, there’s a way to trick the compiler into executing a comment: by using the Unicode character \u000d.
In Java, and in other languages too, \u000d denotes a new line. If we include it in a comment, the compiler will consider everything that follows \u000d to be in the line below the comment and will execute it. Armed with that knowledge, we can attach a cheeky print command, such as:
The next time someone runs the code, this comment will be executed and they will receive a surprise “abracadabra!”