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.
MyClass and myclass are treated as two completely different things.
To understand Java syntax, let's analyze a simple "Hello World" program. In Java, every line of code must reside inside a Class.
public keyword is an access modifier, and class is used to declare a class. The class name (Main) must match the filename (Main.java).System is a class, out is an object, and println() is the method.Java uses specific symbols to organize code and end statements:
{}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.
;Every individual statement or instruction in Java must end with a semicolon. Forgetting a ; is the most common cause of "Compile-time Errors".
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. |
While you can technically name your variables anything, Java developers follow standard "CamelCase" rules to keep code readable.
MyTestClass.myVariableName.com.expertsjava.tutorial.Comments are used to explain the code and are ignored by the compiler. They make the code "Human Readable".
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");
}
}
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 →