Java Tutorial

Java Syntax

Statements end with a semicolon. Blocks use braces. The compiler cares about types, not indentation.

Statements and semicolons

Each statement ends with ;. Forget one and javac stops, often on the next line. Indentation is for people. Braces decide which lines belong together.

Example

public class Main {
  public static void main(String[] args) {
    int n = 4;
    System.out.println(n);
  }
}

Blocks

A block is a pair of braces. main is a block. if and for use blocks too. The compiler does not care how many spaces you use, but a consistent two-space indent keeps examples readable.

Example

public class Main {
  public static void main(String[] args) {
    int score = 18;
    if (score >= 10) {
      System.out.println("pass");
      System.out.println("score was " + score);
    }
  }
}

Both print lines sit inside the if braces, so both run only when the condition is true. Without the braces, only the first line would belong to the if.

Case matters

Main and main are different names. String is the type. string is not. Keywords such as class and public are lowercase.

The method must be named main, not Main. The class can be Main.

Area of a rectangle

Each step is one statement. Without the semicolons javac stops.

Example

public class Main {
  public static void main(String[] args) {
    int length = 4;
    int width = 3;
    int area = length * width;
    System.out.println(area);
  }
}

A missing semicolon is the most common first error. javac often reports it on the next line, because that is where it first notices something is wrong — so when the error points at a line that looks fine, check the line above it.

Try It Yourself

Exercise: This program has two mistakes that stop it compiling. Find them.

public class Main {
  public static void Main(String[] args) {
    int total = 5 + 3
    System.out.println(total);
  }
}
Show solution

The method is spelled Main with a capital M — it must be lowercase main, or the program has no entry point. And the int total = 5 + 3 line is missing its semicolon.

Key Takeaways

  • Every statement ends with a semicolon.
  • Braces group lines into blocks; indentation is for humans, not the compiler.
  • Java is case-sensitive — main and Main are different names.
  • A semicolon error is often reported one line below where it actually is.

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.

Maths

Area of a rectangle

Area is length times width. For a rectangle 4 m by 3 m that is 12 m². Each step is one statement. The semicolon is not decoration; without it javac stops.

A = l × w

Example

public class Main {
  public static void main(String[] args) {
    int length = 4;
    int width = 3;
    int area = length * width;
    System.out.println(area);
  }
}

FAQ: Java Syntax

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 syntax 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 syntax in this Java Java lesson (Java Syntax).

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.