Module lessons (1/3)
Variables, primitive types and structure
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):
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}public class Main: In aMain.javafile, 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):
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)));
}
}intstores 32-bit signed integers (longwith suffixLreaches 64 bits).doublestores double-precision (64-bit) floating point.booleanaccepts onlytrueorfalse(never numeric0or1).charrepresents a single character in single quotes ('A'), unlike"A"which is aString.finalblocks 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:
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:
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
Declare an integer variable called age with value 25, then print it to the screen using System.out.println(age);.
Show hint
Write int age = 25; and on the following line System.out.println(age);.
Solution available after 3 attempts
Declare a variable double price = 19.99; and a variable boolean isAvailable = true;, then print both on separate lines.
Show hint
Define double price = 19.99; and boolean isAvailable = true;, then use two separate System.out.println.
Solution available after 3 attempts
Declare char grade = 'A'; and String student = "John";, then print exactly student + " : " + grade.
Show hint
Use single quotes for char grade = 'A'; and double quotes for String student = "John";.
Solution available after 3 attempts
What value does this Java program print?
int a = 7;
int b = 2;
double ratio = a / b;
System.out.println(ratio);What is the exact output of the following print statement?
System.out.println("Sum: " + 4 + 6);