Java Basic Syntax

Master the structure of Java programs, class names, and the main method.

1. Structure of a Java Program

Every Java program has at least one class. If the class is public, the file name must match the class name (MyFirstJavaProgram.java).

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

What this does (line by line):

  1. public class MyFirstJavaProgram — declares a class named MyFirstJavaProgram.
  2. public static void main(String[] args) — entry point where the JVM begins execution.
  3. System.out.println(...) — prints text to the console and appends a newline.

How the Java Compiler Reads Your Code

Understanding how Java reads your code helps you avoid many beginner mistakes. Java code is read from top to bottom and inside blocks defined by curly braces { }.

Each class, method, loop, or conditional statement creates its own block. Code written outside these blocks is not allowed and will cause compilation errors.

Java also requires strict syntax rules. Every statement must end with a semicolon (;) except for block definitions. Missing even a single semicolon can stop the entire program from compiling.

This strictness may feel uncomfortable at first, but it helps Java catch errors early and makes large applications more reliable.

2. The main() Method — entry point

The JVM looks for a method with the exact signature below to start your program:

public static void main(String[] args)

Meaning of each keyword:

  • public — accessible by the JVM from anywhere.
  • static — JVM calls it without creating an object.
  • void — method returns no value.
  • String[] args — command-line arguments passed by the user.

3. Comments — documenting intent

Comments help other developers (and your future self) understand code. They are ignored by the compiler.

// Single-line comment
/* Multi-line comment
   spanning multiple lines */
/**
 * Javadoc comment — used to generate API docs
 */

Tip: Write short Javadocs for public classes and public methods; it helps users of your API.

4. Keywords — reserved words you cannot use as identifiers

Java has reserved words (keywords) that carry special meaning. Examples:

class, public, static, void, if, else, for, while, try, catch, return

Attempting to use a keyword as a variable or method name will cause a compile-time error.

5. Naming Conventions

  • Classes: PascalCase — MyClass
  • Methods: camelCase — calculateTotal()
  • Variables: camelCase — totalAmount
  • Constants: UPPER_SNAKE_CASE — MAX_RETRIES

Following these conventions improves readability and is expected in professional code.

Java Is Case-Sensitive

Java is a case-sensitive language. This means uppercase and lowercase letters are treated as different characters.

For example, the following identifiers are NOT the same in Java:

String name;
String Name;
String NAME;

All three variables above are considered different by the Java compiler. Beginners often face errors simply because of incorrect letter casing.

Keywords such as class, public, and static must always be written in lowercase. Writing Class or Public will cause compilation errors.

6. Simple Runnable Example

Try this interactive example:

Run in

public class PrintingDemo {
    public static void main(String[] args) {
        int age = 25;
        String name = "Raju";
        System.out.println(name + " is " + age + " years old.");
    }
}

7. Common beginner mistakes

  • Not matching public class name with file name (e.g., class MyApp must be in MyApp.java).
  • Forgetting main method signature — JVM won't find your program entry point.
  • Missing semicolons at the end of statements (;).
  • Using keywords as variable names.

8. Quick practice (copy, paste & run)

Copy this into your Java file (TestDemo.java) and run it using javac and java:

Or try this example with

public class TestDemo {
    public static void main(String[] args) {
        int n = 5;
        System.out.println("Numbers 1 to " + n + ":");
        for (int i = 1; i <= n; i++) {
            System.out.println(i);
        }
    }
}

Whitespace, Indentation, and Code Readability

Java ignores extra spaces, tabs, and line breaks. However, how you format your code is extremely important for readability and maintenance.

Consider the following two examples. Both work exactly the same, but only one is readable:

// Bad formatting
public class Test{public static void main(String[]args){System.out.println("Hello");}}
// Good formatting
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Always use proper indentation and line breaks. This is not just a style preference — it is expected in professional Java development and code reviews.

Quick core takeaways

  • Java programs are organized in classes. A public class filename must match the class name.
  • The JVM starts execution from the public static void main(String[] args) method.
  • Comments are for humans and ignored by the compiler; use them to explain intent.
  • Follow naming conventions (PascalCase for classes, camelCase for methods/variables).