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!
Consider the following well-known example, in which the system fails to see that two sets of characters are identical:
Even though
Now, scale this example up and consider a JSON parser implementation — where fields such as
What a waste of memory!
We can get around this by invoking
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:
In C, C++ and other programming languages, we often use the command
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
This second example shows how we can use label breaks to interrupt a try block (surprising right?!):
If the
And if it returns *false*, we’ll get:
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:
The
In the example below,
Note:
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.
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
In Java, and in other languages too,
The next time someone runs the code, this comment will be executed and they will receive a surprise “abracadabra!”
This content is for informational purposes only. It doesn't necessarily reflect the views of Navan and should not be construed as legal, tax, benefits, financial, accounting, or other advice. If you need specific advice for your business, please consult with an expert, as rules and regulations change regularly.