Java
Programming
Software Development
Coding Tips
Java Hidden Features

Hidden Features of Java

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When people say "hidden features of Java," they often mean language or library features that are powerful but easy to overlook, not secret syntax. The most useful ones are usually not the flashy tricks; they are the features that reduce boilerplate, improve correctness, or make core collections work more effectively.

Map.computeIfAbsent for Lazy Initialization

A lot of old Java code manually checks whether a map already contains a key before creating a value. computeIfAbsent compresses that pattern into one operation.

java
1import java.util.ArrayList;
2import java.util.HashMap;
3import java.util.List;
4import java.util.Map;
5
6Map<String, List<String>> groups = new HashMap<>();
7groups.computeIfAbsent("fruit", key -> new ArrayList<>()).add("apple");
8groups.computeIfAbsent("fruit", key -> new ArrayList<>()).add("banana");
9
10System.out.println(groups);

This is especially handy for grouping, indexing, and adjacency-list style structures.

EnumSet and EnumMap Are Better Than General Collections for Enums

When the keys or values are enums, the specialized collections are often cleaner and faster than generic HashSet or HashMap.

java
1import java.util.EnumSet;
2
3enum Permission {
4    READ, WRITE, DELETE
5}
6
7EnumSet<Permission> allowed = EnumSet.of(Permission.READ, Permission.WRITE);
8System.out.println(allowed.contains(Permission.DELETE));

These types are worth knowing because they communicate intent and use the enum domain efficiently.

Try-With-Resources Does More Than Close Files

Many developers first learn try-with-resources for file I/O, but it applies to any AutoCloseable resource.

java
1import java.io.BufferedReader;
2import java.io.StringReader;
3
4try (BufferedReader reader = new BufferedReader(new StringReader("hello"))) {
5    System.out.println(reader.readLine());
6}

This feature is one of the simplest ways to reduce cleanup bugs and exception-path leaks.

Text Blocks Improve Multi-Line String Readability

Multi-line strings used to require awkward concatenation. Text blocks make embedded SQL, JSON, and test fixtures much easier to read.

java
1String query = """
2    SELECT id, name
3    FROM users
4    WHERE active = true
5    ORDER BY name
6    """;
7
8System.out.println(query);

This is not a hidden feature in the sense of obscurity, but it is often underused by teams that still write Java as if it were several language versions older.

Records Are Small Data Carriers Without Boilerplate

When the goal is to represent immutable data, records can remove a lot of ceremony.

java
1record UserSummary(long id, String name) {}
2
3UserSummary user = new UserSummary(1L, "Ana");
4System.out.println(user.name());

This is often a better fit than writing a full class with fields, constructor, accessors, equals, hashCode, and toString by hand.

Be Careful with Clever Tricks

Some so-called hidden Java features are really code smells in disguise. Double-brace initialization is a classic example. It looks concise, but it creates anonymous classes and can surprise readers.

The useful hidden features of Java are the ones that improve maintainability, not the ones that merely look clever in a code snippet.

A useful rule is to treat these features as tools for reducing ceremony, not for showing off language knowledge. If the feature makes the code easier to read for the next Java developer, it is probably worth using; if it merely looks clever, it usually is not.

Common Pitfalls

Ignoring newer standard-library features often leads teams to keep rewriting patterns that Java already supports directly.

Using obscure tricks for brevity can make code less maintainable than the boilerplate they replaced.

Learning features without matching them to the right use case turns good tools into unnecessary complexity.

Summary

  • Useful Java "hidden features" are usually practical library and language tools, not gimmicks.
  • 'computeIfAbsent, EnumSet, and try-with-resources solve recurring real-world problems.'
  • Text blocks and records can remove a lot of boilerplate in modern Java.
  • Prefer features that improve clarity and correctness over tricks that only look clever.

Course illustration
Course illustration

All Rights Reserved.