HOME HTML EDITOR C JAVA PHP

Java Syntax: The Building Blocks

Every programming language has its own set of rules, known as Syntax. In Java, the syntax is strict, case-sensitive, and follows a clear structural hierarchy.

Warning: Java is Case-Sensitive. This means MyClass and myclass are treated as two completely different things.

1. The Anatomy of a Java Program

To understand Java syntax, let's analyze a simple "Hello World" program. In Java, every line of code must reside inside a Class.

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

Key Components Explained:

2. Curly Braces {} and Semicolons ;

Java uses specific symbols to organize code and end statements:

Curly Braces {}

They define the beginning and end of a block of code (like a class or a method). Always ensure every opening brace has a matching closing brace.

Semicolons ;

Every individual statement or instruction in Java must end with a semicolon. Forgetting a ; is the most common cause of "Compile-time Errors".

3. Decoding the Main Method Syntax

Why is the main method so long? Let's break down each keyword:

Keyword Technical Meaning
public The method can be called from anywhere outside the class.
static The method belongs to the class itself, not an object. JVM can call it without creating an instance.
void The method does not return any data (like a number or string).
String[] args An array of strings used for command-line arguments.

4. Java Naming Conventions

While you can technically name your variables anything, Java developers follow standard "CamelCase" rules to keep code readable.

5. Java Comments

Comments are used to explain the code and are ignored by the compiler. They make the code "Human Readable".

// This is a Single-line Comment /* This is a Multi-line Comment for longer explanations */ /** This is a Documentation Comment Used for creating API docs */

6. White Space and Indentation

Java doesn't care about spaces (white space). However, Indentation (tabs) is crucial for readability. It helps you see which code belongs inside which block.

Bad Practice (Messy):

public class Main{public static void 
main(String[] args){System.out.println("Hi");}}

Good Practice (Clean):

public class Main {
    public static void main(String[] args) {
        System.out.println("Hi");
    }
}

Ready for Logic?

Now that you know the skeleton of a Java program, it's time to add some "Data". In the next chapter, we will learn about Output and how to display different types of data to the user.

Next: Java Output →