var and final
var lets the compiler infer a local type. final means the variable cannot be reassigned, and constants are written static final in uppercase.
Example
var list = new java.util.ArrayList<String>();
list.add("x");
final int LIMIT = 10;
System.out.println(list + " " + LIMIT);
var n = 3.5;
System.out.println(n);Result
[x] 10 3.5
Exercise
Prevent reassignment.
int N = 5;Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.