Navan Tech Blog
7 Java Features You Might Not Have Heard Of

7 Java Features You Might Not Have Heard Of

Ronny Balaga

16 Mar 2023
5 minute read
7 Java Features You Might Not Have Heard Of
It’s hard to keep up with Java’s frequent updates, especially the more obscure features; highlighted here are a few tips and tricks you may want to add to your toolkit.

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!

1. Intern

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:

7 Java Features - Code Snippet 1

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.

7 Java Features - Code Snippet 2

2. Double-brace initialization

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:

7 Java Features - Code Snippet 3

3. Labeled breaks and continues

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:

7 Java Features - Code Snippet 4

Calling firstNumsToSum(List.of(0,1,2), List.of(1,2,3), 3) will print:

7 Java Features - Code Snippet 5

This second example shows how we can use label breaks to interrupt a try block (surprising right?!):

7 Java Features - Code Snippet 6

If the needToUpdateRecord step returns *true*, the output will be:

7 Java Features - Code Snippet 7

And if it returns *false*, we’ll get:

7 Java Features - Code Snippet 8

4. Java files can contain multiple non-nested classes

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:

7 Java Features - Code Snippet 9

5. DelayQueue

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.

7 Java Features - Code Snippet 10

Note:

  • DelayQueue implements the BlockingQueue interface therefore it is thread-safe
  • DelayQueue is an unbounded queue
  • The element with the shortest remaining delay time appears at the head of the queue
  • An element is expired when its getDelay()method returns a value less than or equal to zero
  • Null is not allowed on DelayQueue

6. Delimiter long numbers with underscores

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.

7 Java Features - Code Snippet 11

7. Easter egg: Executable comments

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:

7 Java Features - Code Snippet 12

The next time someone runs the code, this comment will be executed and they will receive a surprise “abracadabra!”

Return to blog

More content you might like