Declare typed variables, learn Java’s primitive types versus objects, understand scope, and convert between types safely with casting.
Why: Java is statically typed — every variable has a type you declare up front, and it can only ever hold that type. You write the type, then the name, then the value. Names use lowerCamelCase by convention.
int age = 30;
String firstName = "Ada";
boolean isActive = true;
// reassign freely, but only with the same type
age = age + 1; // 31
System.out.println(age);Why: inside a method you can write var and Java figures out the type from the value on the right. It is still strongly typed — age is still an int, you just typed less. Use it when the type is obvious from the value.
var age = 30; // inferred as int
var name = "Ada"; // inferred as String
var prices = new double[]{ 1.5, 2.0 }; // inferred as double[]
// var needs a value to infer from — this is NOT allowed:
// var x; // compile errorWhy: Java has separate types for whole numbers and decimals. int is the everyday whole number; long holds bigger whole numbers (suffix L); double is the everyday decimal. Integer division throws away the remainder, which surprises beginners.
int count = 10;
long bigNumber = 9_000_000_000L; // underscores are just for readability
double price = 19.99;
System.out.println(7 / 2); // 3 (int division drops the decimal!)
System.out.println(7 / 2.0); // 3.5 (one double makes it a double)
System.out.println(7 % 2); // 1 (remainder)Why: a boolean is true or false. A char is a single character in single quotes. A String is text in double quotes — and unlike the others, String is an object, not a primitive, which is why it gets a capital S.
boolean ready = false;
char grade = 'A'; // single quotes, exactly one character
String message = "Hello"; // double quotes, an object
System.out.println(message.length()); // 5 — Strings have methodsWhy: the lowercase types (int, double, boolean, char) are primitives — raw values stored directly. Each has an object version with a capital letter (Integer, Double, Boolean). Collections and generics can only hold objects, so Java auto-converts between them ("autoboxing") when needed.
int primitive = 42;
Integer object = 42; // autoboxing: int -> Integer automatically
// the object versions carry useful helpers
System.out.println(Integer.parseInt("100") + 1); // 101
System.out.println(Integer.MAX_VALUE); // 2147483647Why: a variable only exists inside the { } block where it is declared. Once that block ends, the variable is gone. This keeps names from leaking and clashing across different parts of your code.
int total = 0; // visible for the whole method
if (total == 0) {
int bonus = 5; // only exists inside this if-block
total = total + bonus;
}
// System.out.println(bonus); // ERROR: bonus is out of scope here
System.out.println(total); // 5Why: casting converts a value from one type to another. Going from a smaller type to a bigger one (int to double) is automatic and safe. Going the other way (double to int) must be written explicitly with (type) and loses the decimal part.
int whole = 7;
double asDecimal = whole; // automatic widening: 7.0
double pi = 3.99;
int truncated = (int) pi; // explicit narrowing: 3 (NOT rounded)
// text to number uses a parse method, not a cast
int parsed = Integer.parseInt("123");
System.out.println(parsed + 1); // 124