Java Syntax – Your First Step into Writing Java Code
- Krishnamohan Yagneswaran
- 1 day ago
- 2 min read
Understanding Java syntax is essential for anyone beginning their journey into programming with this powerful language. In this section, we’ll break down a simple Java program and explain each component so you can start writing code confidently.
💻 A Simple Java Program Example
Here’s the first Java program most developers write when starting out:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
This code is stored in a file named Main.java.
🔍 Understanding the Code Structure
Let’s take a closer look at what each part of this program does:
1. Class Declaration
public class Main {
Every Java application begins with a class.
The class name (in this case, Main) must start with an uppercase letter.
The file name should also match the class name (i.e., Main.java).
✅ Java is case-sensitive: MyClass and myclass are not the same.
2. The main() Method
public static void main(String[] args)
This method is the entry point of any Java program.
The main() method must always be present and written exactly this way.
Java automatically executes the code inside this method when you run your program.
💬 3. Printing Output
System.out.println("Hello World");
This line prints text to the screen.
System is a built-in class.
out is a static object within System.
println() is the method used to display messages.
📝 Don’t worry about how System.out.println() works internally—just know it’s essential for output.
🔐 Curly Braces and Semicolons in Java
Curly braces {} define the beginning and end of a block of code (like inside classes and methods).
Each statement must end with a semicolon (;), which tells Java where an instruction ends.
🧰 Setting Up and Running Java
To run Java programs on your machine:
Make sure Java is installed correctly.
Save your file as Main.java.
Use the command line or an IDE to compile and execute it.
🟢 Expected Output:
Hello World
Summary: Key Rules of Java Syntax
All code must reside inside a class.
The class name should match the filename.
Every Java application requires a main() method.
Use System.out.println() for displaying output.
Follow syntax rules like semicolons and case sensitivity strictly.
👉 Ready for the next step?
Check out the Java Output and Java Comments guides to learn how to format your program’s results and document your code effectively.
Comments