Return to site

☕🆔 JAVA 25 UNNAMED VARIABLES: WRITE LESS, SAY MORE

· java,java25

🔸 TL;DR

Java 25 lets you use _ as an unnamed variable in places where you don’t care about the value (loop index, lambda param, catch param…). It makes your intent clearer: “I need the side-effect, not the value.” ✅ Less noise, more expressive code.

Section image

🔸 WHAT ARE UNNAMED VARIABLES?

▪️ Use _ as the name of a local variable, exception, or lambda parameter when you don’t need to read it.

▪️ The variable is declared but has no usable name – you can’t read it later in the code.

▪️ Perfect when the side effect of the statement matters more than the value itself.

🔸 SIMPLE EXAMPLE

Before: unused loop variable 👇

int[] orderIDs = {34, 45, 23, 27, 15};

int total = 0;

for (int id : orderIDs) {

total++;

}

System.out.println("Total: " + total);

After: say “I don’t care about the element” 👇

int[] orderIDs = {34, 45, 23, 27, 15};

int total = 0;

for (int _ : orderIDs) {

total++;

}

System.out.println("Total: " + total);

Same behavior, clearer intent 🧠

🔸 WHERE CAN YOU USE _?

(Exact list depends on the JDK version / JEP, but conceptually 👇)

▪️ Enhanced for-loops when you just want to count / trigger side effects.

▪️ Lambda parameters you don’t use (e.g. event handlers where you ignore an arg).

▪️ Catch parameters when the type matters but not the variable name.

▪️ Some local variables whose value you intentionally ignore.

🔸 WHY IT’S USEFUL

▪️ Makes unused values explicit instead of “oops, I forgot to use it”.

▪️ Reduces IDE warnings and noisy names like ignored, unused, _1.

▪️ Improves readability: other devs see at a glance what matters.

▪️ Encourages a style focused on effects & intent, not boilerplate.

🔸 TAKEAWAYS

▪️ Java 25 leans into expressive, concise code with unnamed variables.

▪️ Use _ when you truly don’t need the value — not out of laziness.

▪️ Combine with modern Java features (records, pattern matching, etc.) for a cleaner, more intentional codebase.

#️⃣ s

#Java25 #Java #UnnamedVariables #ModernJava #CleanCode #DeveloperExperience #JVM #CodeReadability #BackendDevelopment #JavaTips