Java Tutorial
Java Get Started
Install a JDK locally, or use the StudyGrid Java editor. Either way you need a .java file and a main method.
Two ways to run Java here
The StudyGrid editor compiles in the browser. You do not install a JDK for the lessons. If you want the same loop on your machine, install a JDK 17 or 21 and use the terminal commands below.
A file named Main.java
The public class name must match the file name. Lessons on this site use public class Main so every example opens in the same editor tab.
Example
public class Main {
public static void main(String[] args) {
System.out.println("ready");
}
}Compile locally
In a terminal, from the folder that holds the file:
Example
javac Main.java
java MainOn StudyGrid, press Compile & run instead. The editor is /java/try.
What you need in every program
- A class.
- A
public static void main(String[] args)method. - Statements that end with a semicolon.
Compiling locally, a public class must live in a file of the same name:public class Main belongs in Main.java. A mismatch gives "class Main is public, should be declared in a file named Main.java" — a confusing first error with a simple fix.
The line-by-line meaning of the boilerplate
| Part | What it means |
|---|---|
public class Main | Names the program; must match the file name. |
public static void main | The one method Java runs first, automatically. |
String[] args | Words typed after the program name on the command line. |
System.out.println(...) | Prints a line to the console. |
Do not worry about memorising every word yet — you will type it enough times that it sticks on its own.
A second program: two prints
Once main is in place, add as many statements as you need. Each ends with a semicolon. This program prints a label, then a number.
Example
public class Main {
public static void main(String[] args) {
System.out.println("tickets");
System.out.println(12);
}
}Try It Yourself
Exercise: Create Main.java that prints ready and thenok on two lines. Compile and run it in the editor.
Show solution
public class Main {
public static void main(String[] args) {
System.out.println("ready");
System.out.println("ok");
}
}If the class is not named Main, rename the file to match, or rename the class.
Key Takeaways
- Every program needs a class and a
mainmethod — that is where Java starts. - Locally:
javac Main.javacompiles,java Mainruns. On StudyGrid, press Compile & run. - A
publicclass name must match its file name. - Statements end with a semicolon; braces mark the block that belongs to
main.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Label before you weigh
Copper(II) sulfate pentahydrate is CuSO4·5H2O. If two bottles sit on the bench, the label is the only thing that stops you using the anhydrous salt by mistake.
A Java file starts in main. The first useful line is often that label, printed so the rest of the session has a name.
Example
public class Main {
public static void main(String[] args) {
System.out.println("Sample: CuSO4.5H2O");
System.out.println("Ready to weigh.");
}
}Engineering
A board saying it booted
Embedded work is often still Java on a gateway, even when the chip is small. After reset, the first human-readable line on a serial port is how you tell a dead board from a live one.
Nothing is measured yet. The point of the listing is the compile-run loop: a .java file, a main, stdout.
Example
public class Main {
public static void main(String[] args) {
System.out.println("logger v1: ok");
}
}