Skip to main content
Module 1 · Lesson 1 of 31/22 in the course~12 min
Module lessons (1/3)

Variables, primitive types and structure

Warm-Up Stage

First program on the JVM

Write statically typed Java code by reading and predicting each type before compiling.

Java is a strongly and statically typed language: each variable knows its type at compile time (javac) and each instruction lives inside a class executed by the JVM (Java Virtual Machine).

1. Anatomy of a Minimal Java Program

Every executable program starts from a method with exact signature public static void main(String[] args):

Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}
  • public class Main: In a Main.java file, the public class name exactly matches the file name.
  • public static void main(String[] args): This is the entry point looked for by the JVM at startup.
  • System.out.println(...): Print text on standard output by adding a carriage return (print(...) stays on the same line).

2. Primitive Types, final and Inference var

Java separates primitive types (direct values on the Stack) from reference types (objects on the Heap like String):

Java
public class Main {
    public static void main(String[] args) {
        int age = 25;
        double unitPrice = 19.99;
        boolean isAvailable = true;
        char grade = 'A';
        final double TAX_RATE = 0.22;
        var label = "Starter Pack"; // Compilato esattamente come String

        System.out.println(label + " -> " + (unitPrice * (1 + TAX_RATE)));
    }
}
  • int stores 32-bit signed integers (long with suffix L reaches 64 bits).
  • double stores double-precision (64-bit) floating point.
  • boolean accepts only true or false (never numeric 0 or 1).
  • char represents a single character in single quotes ('A'), unlike "A" which is a String.
  • final blocks reassignment after initial initialization.

3. Full Division and Explicit Casting

Dividing two int always produces a truncated result to integer (5 / 2 == 2), because the JVM evaluates the operator before looking at the target variable:

Java
public class Main {
    public static void main(String[] args) {
        int total = 5;
        int count = 2;

        double wrongAvg = total / count;          // 2.0 (troncato PRIMA di assegnare!)
        double exactAvg = (double) total / count; // 2.5 (cast su un operando promuove l'altro)
        System.out.println(wrongAvg + " vs " + exactAvg);
    }
}

4. Concatenation precedence with + and Conversions

When a + meets a String, everything that follows becomes text concatenation from left to right:

Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Totale: " + 2 + 3);     // Stampa "Totale: 23"!
        System.out.println("Totale: " + (2 + 3));   // Stampa "Totale: 5"

        int parsed = Integer.parseInt("42");
        System.out.println(parsed + 8);             // 50
    }
}

Try it yourself

Exercise#java.m1.l1.e1
Attempts: 0Loading…

Declare an integer variable called age with value 25, then print it to the screen using System.out.println(age);.

Loading editor…
Show hint

Write int age = 25; and on the following line System.out.println(age);.

Solution available after 3 attempts

Exercise#java.m1.l1.e2
Attempts: 0Loading…

Declare a variable double price = 19.99; and a variable boolean isAvailable = true;, then print both on separate lines.

Loading editor…
Show hint

Define double price = 19.99; and boolean isAvailable = true;, then use two separate System.out.println.

Solution available after 3 attempts

Exercise#java.m1.l1.e3
Attempts: 0Loading…

Declare char grade = 'A'; and String student = "John";, then print exactly student + " : " + grade.

Loading editor…
Show hint

Use single quotes for char grade = 'A'; and double quotes for String student = "John";.

Solution available after 3 attempts

Quiz#java.m1.l1.e4
Ready

What value does this Java program print?

Java
int a = 7;
int b = 2;
double ratio = a / b;
System.out.println(ratio);
Answer options
Quiz#java.m1.l1.e5
Ready

What is the exact output of the following print statement?

Java
System.out.println("Sum: " + 4 + 6);
Answer options