SwadhyAI Notes

All Java notes

Java

Java syntax

›Short versionOpen if you only have a minute.
  • A public class and its file share one spelling. Main.java holds public class Main, capitals included.
  • The launcher starts at public static void main(String[] args), not at the first line of the file.
  • A statement ends with a semicolon. Without it, the compiler keeps reading the next line as the same instruction.
  • A class body and a method body are blocks. They close with braces, not with a semicolon.
  • Indentation does not change the meaning. It shows a person which block a line belongs to.

Save this as Main.java and run it. The console, the text window attached to the program, prints Hello World, and then the program stops.

How a file is read

1

File name

The public class and Main.java share one spelling.

2

Entry

The launcher starts at main, not at line one.

3

Statements

Each instruction ends with a semicolon.

4

Blocks

Class and method bodies close with braces.

Names the compiler checks

  • public class Main and Main.java must be the same identifier, capitals included.
  • Keywords such as class and void are reserved. Identifiers such as Main and println are yours.

Boundaries it expects

  • Statements end with semicolons. Blocks end with matching braces.
  • Indentation helps people read nesting. The compiler ignores extra spaces for meaning.

Keyword, identifier, or punctuation

TokenRoleExample in Main.java
classKeyword: starts a class declarationpublic class Main
MainIdentifier: name of the classpublic class Main
mainIdentifier: name of the entry methodpublic static void main(...)
;Ends a statementprintln("Hello World");
{ }Opens and closes a blockclass body and main body
javac checks before it writes a class file
Diagram text: the exact Mermaid definition below preserves the nodes, labels, and connections. Enable JavaScript to view the rendered diagram.
Read the diagram definition
sequenceDiagram
  participant You
  participant javac
  participant File as Main.java
  participant Out as Main.class
  You->>javac: Main.java
  javac->>File: public class matches file name?
  alt name mismatch
    javac-->>You: error, no .class file
  else legal source
    javac->>File: semicolons and braces balance
    javac-->>Out: write bytecode
    javac-->>You: ready to run
  end
java
public class Main {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

The file is short because each part has one job. It names a , it names the where running begins, and it gives that method a single to carry out. Main, main, and println are : names, with the capital letters kept as part of the name.

The is picky about those jobs because it only sees names, braces, and the mark that ends an instruction. When a semicolon is missing, it stops because it cannot tell where the instruction ended. It will not guess that you meant to finish the line.

The class is public class main inside a file named Main.java. Why does javac refuse it?

The public class name and the file name are the same word, capitals included. main and Main are different names. Rename the class to Main, or rename the file to match the class you actually declared.

The class and the file

public class Main opens the class whose name is Main. class is a . It announces that a class is being declared, so you cannot also use it as a name of your own. public lets the launcher see the class. The braces after the name hold everything that belongs to Main.

That name has to be the file name. This class lives in Main.java. Leave the file called Main.java and write public class main, and the compiler rejects it. Main and main are different identifiers. A capital letter is part of the spelling.

A clearer name follows the same rule. A program that adds numbers can be public class Calculator in Calculator.java. The public class and the file still share one spelling, capitals included.

Where the program starts

Running a class does not begin at the first line of the file. The launcher looks for a method written in this shape, and it starts there.

java
public static void main(String[] args)

public lets the launcher call it. static lets it run before any object of Main exists. void means the method does not hand a value back. main is the name the launcher searches for. String[] args is the list of extra words typed after the program name on the command line, and that list may be empty. Those words appear in this order because a method with another name, or another shape, is a different method. The launcher will not start it.

The instructions you want to run go inside the method, between its braces. That region is a . The class has a block of its own around the method. Class and method declarations close with braces. They do not take a semicolon.

One instruction, then a stop

System.out.println("Hello World"); asks the println method to write the text and then start a new line. System is a class the language already provides. out is the stream that writes to the console. The semicolon belongs to the statement. It is how the compiler knows the instruction is over.

This file is almost the same program, saved as HelloWorld.java, with the first instruction left open.

java
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello")
    System.out.println("World");
  }
}

The class name matches the file, and the second println is finished. The first one never closed. The compiler reaches System.out.println("World") still inside the previous instruction, so the error usually points at that later line. The repair is a semicolon after "Hello".

Several finished statements run from top to bottom. Each println writes its text and a newline, so the lines stack.

java
System.out.println("Hello World");
System.out.println("I am learning Java");
System.out.println("It is awesome!");

What the console shows

Hello World
I am learning Java
It is awesome!

Java output stays with println, and with print, which writes text without moving to the next line.

Braces, semicolons, and case

Put the boundaries back together and the file compiles. Saved as MyClass.java, the public class name matches, main has both braces, and the statement ends.

java
public class MyClass {
  public static void main(String[] args) {
    System.out.println("Hello");
  }
}

The same idea with those boundaries broken:

java
public class myclass {
public static void main(String[] args)
System.out.println("Hello")
}

If the file is MyClass.java, myclass is the wrong public class name, because the case does not match. main never opens a block, so the print instruction is not inside the method. The instruction has no semicolon, so the compiler cannot see where it ends. The class's closing brace then arrives while the method is still open.

Indentation leaves the meaning alone. The compiler accepts the spaced MyClass file and would accept the same tokens pushed against the left edge, provided the name, the braces, and the semicolon are intact. The spaces are there so a person can see which block a line belongs to. A missing brace is much easier to find when the inner lines sit one step to the right.

What counts as a finished instruction, and which forms use a semicolon while if and loops use braces, continues on Java statements.

Simple rule of thumb

Match the public class name to the file name before you chase smaller errors. Finish every statement with a semicolon and every block with a brace. The compiler will not infer what you meant from spacing alone.

Good default: one public class per file, name the file after that class, start execution in public static void main(String[] args), and end each println with a semicolon inside balanced braces.