var, final & Wrappers in Java

From the Java cheat sheet · Variables & Data Types · verified Jul 2026

var, final & Wrappers

Local type inference, constants, casting, and boxed types.

java
// Quick Reference
var name = "Sam";        // inferred as String
final int MAX = 100;     // constant, cannot reassign
Integer boxed = 42;      // wrapper object (nullable)
💡 var only works for local variables with an initializer, not fields or params.
⚡ Wrapper types (Integer, Double) can be null; primitives cannot.
📌 Prefer primitives for performance; use wrappers when you need null or generics.
🟢 Narrowing casts (double -> int) truncate and must be explicit with (type).
varfinalwrappers

More Java tasks

Back to the full Java cheat sheet