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.

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.

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);
        }
    }
}

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).
Sidebar Ad Space
Advertisement Space