Understand Java's statically-typed variable system and primitive data types.
1public class Variables {2 public static void main(String[] args) {3 // Primitive types4 int age = 21;5 double gpa = 3.85;6 boolean isStudent = true;7 char grade = 'A';8 9 // Reference type10 String name = "Alice";11 12 // Constants13 final double PI = 3.14159;14 15 System.out.println(name + " is " + age + " years old.");16 System.out.println("GPA: " + gpa);17 }18}Alice is 21 years old. GPA: 3.85
1public class Casting {2 public static void main(String[] args) {3 // Widening (automatic)4 int myInt = 42;5 double myDouble = myInt; // int → double6 System.out.println(myDouble); // 42.07 8 // Narrowing (manual)9 double pi = 3.14159;10 int rounded = (int) pi; // double → int11 System.out.println(rounded); // 312 13 // String to int14 String numStr = "100";15 int num = Integer.parseInt(numStr);16 System.out.println(num + 50); // 15017 }18}42.0 3 150