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 Main

On 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

PartWhat it means
public class MainNames the program; must match the file name.
public static void mainThe one method Java runs first, automatically.
String[] argsWords 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 main method — that is where Java starts.
  • Locally: javac Main.java compiles, java Main runs. On StudyGrid, press Compile & run.
  • A public class 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");
  }
}

FAQ: Java Get Started

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java get started examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java get started in this Java Java lesson (Java Get Started).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.