Lesson 2 of 20
Variables & Types
Variables and Primitive Types
Java is statically typed — every variable has a declared type:
int age = 25; // 32-bit integer
long population = 8_000_000_000L; // 64-bit integer
double pi = 3.14159; // 64-bit floating point
boolean active = true; // true or false
char grade = 'A'; // single character
String
String is a class (not a primitive), but behaves like one for most purposes:
String name = "Alice";
String greeting = "Hello, " + name + "!"; // concatenation
Type Inference with var
Since Java 10, you can use var to let the compiler infer the type:
var score = 100; // inferred as int
var message = "Hi"; // inferred as String
var is most useful when the type is obvious from the right-hand side or when the full type name is long and repetitive (e.g., var users = new ArrayList<String>() instead of ArrayList<String> users = new ArrayList<>()). Avoid var when the type isn't clear from context — explicit types make code easier to read. Note that var can only be used for local variables, not for fields, method parameters, or return types.
Constants
Use final to declare a constant:
final double TAX_RATE = 0.08;
Your Task
Declare the following variables and print the output exactly as shown:
name="Alice",age=25,height=1.75,student=true- Print:
Alice is 25 years old - Print:
Height: 1.75 - Print:
Student: true
TeaVM (WASM) loading...
Loading...
Click "Run" to execute your code.