Java Programming from Scratch

Java Programming from Scratch

Java is widely used in enterprise systems, Android and servers. Its strength is that one program runs on many operating systems: the source is compiled to bytecode and runs on the JVM (Java Virtual Machine). This article walks you through your first program, variables, conditions, loops and classes, then compiling and running it yourself.

Every example here was compiled and run with Java 17, and the output shown is real.

What you need

How it works

Diagram of the workflow: a .java file is compiled by javac into .class bytecode, which java runs on the JVM
Diagram of the workflow: a .java file is compiled by javac into .class bytecode, which java runs on the JVM

Three steps: write a `.java` file, compile it with `javac` to get a `.class` file, then run it with `java`.

Your first program

Create `Hello.java`. The class name must match the file name. Type this code:

public class Hello {
    public static void main(String[] args) {
        System.out.println("สวัสดี Java");
    }
}

Compile and run:

javac -encoding UTF-8 Hello.java
java -Dfile.encoding=UTF-8 Hello

Output:

สวัสดี Java

`main` is the starting point of the program. `System.out.println` prints text. The `-encoding UTF-8` option makes sure Thai text in the file is read correctly.

Variables and types

Java is statically typed: you declare the type of every variable.

TypeStoresExample
intwhole numbers`int age = 20;`
doubledecimal numbers`double price = 19.5;`
booleantrue or false`boolean passed = true;`
Stringtext`String name = "สมชาย";`

Conditions and loops

This example has a `Student` class that decides a grade from a score, and a `for` loop that walks through a list of students.

import java.util.ArrayList;
import java.util.List;

public class Hello {
    static class Student {
        String name;
        int score;

        Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        String grade() {
            if (score >= 80) return "A";
            else if (score >= 70) return "B";
            else if (score >= 50) return "C";
            return "F";
        }
    }

    public static void main(String[] args) {
        System.out.println("สวัสดี Java");

        List<Student> students = new ArrayList<>();
        students.add(new Student("สมชาย", 85));
        students.add(new Student("สมหญิง", 72));
        students.add(new Student("สมศักดิ์", 45));

        int total = 0;
        for (Student s : students) {
            System.out.println(s.name + " ได้เกรด " + s.grade());
            total += s.score;
        }
        System.out.println("คะแนนเฉลี่ย: " + (total / students.size()));
    }
}

Real output:

สวัสดี Java
สมชาย ได้เกรด A
สมหญิง ได้เกรด B
สมศักดิ์ ได้เกรด F
คะแนนเฉลี่ย: 67

Things to notice:

Common beginner mistakes

Next steps

Try a small calculator, store a list with `ArrayList`, and split your code into several classes in several files, before moving on to web development with Spring Boot.

Summary

Java compiles source code and runs it on the JVM. The starting point is the `main` method, variables have explicit types, and classes organise the program.