Java basics

The structure of a Java program, printing and input, variables and types, and decisions and loops.

About 5 hours of study

Learning objectives

Lessons

  1. 1

    Your first Java program

    How a Java program is laid out, how javac and the JVM turn it into a running program, printing with println, print and printf, and reading input with Scanner.

  2. 2

    Variables and types

    Java's primitive types, int and long, double, char and boolean, String, declaring variables with types or var, constants with final, casts, integer division, and overflow.

  3. 3

    Decisions and loops

    if, else if and else, comparison and logical operators, the switch statement and the newer switch expression, and for, while, do-while and enhanced for loops with break and continue.

Module practice

Module quiz

1. Which method does Java run first?
2. What is 7 / 2 in Java when both are int?
3. Which loop always runs its body at least once?
4. Which type holds 3,000,000,000 without overflow?
5. What does a switch expression with -> avoid?
6. Why does (int) 3.99 give 3?

Methods, arrays and strings ›