When var Helps Readability
Good and bad uses of var.
var Is a Style Choice
var is optional. Used well it makes code cleaner; used carelessly it hides important information. Good developers learn when to reach for it.
Good: Obvious Right-Hand Side
When the initializer clearly shows the type, var removes noise without losing clarity.
public class Main {
public static void main(String[] args) {
var name = "Alice";
var customers = new java.util.ArrayList<String>();
customers.add(name);
System.out.println(customers);
}
}All lessons in this course
- Using var for Locals
- When var Helps Readability
- var in for Loops
- Limitations of var