Java Fundamentals
Java Syntax
The Anatomy of a Java Program
Every Java program has a specific structure. Think of it like a house. It needs a foundation, walls, and a door to enter through. In Java, the main container for your code is called a class.
Inside this class, you need an entry point—the place where the program starts running. This is called the main method. Let's look at the simplest Java program, the classic "Hello, World!".
public class HelloWorld {
// This is the main method, the program's starting point.
public static void main(String[] args) {
// This line prints "Hello, World!" to the console.
System.out.println("Hello, World!");
}
}
Let’s break that down:
-
public class HelloWorld: This line declares a class namedHelloWorld. Thepublickeyword means it can be accessed by other classes. For now, just know that your main program structure will live inside a class. -
public static void main(String[] args): This is the full signature of themainmethod. It’s a lot to take in, but it's the standard entry point for any Java application. The program looks for this exact method to begin execution. -
System.out.println("Hello, World!");: This is a statement. It tells the system to print the text inside the parentheses to the console. After printing, it moves the cursor to a new line.
Rules of the Road
Java has a few syntax rules that are like grammar for the language. They aren't suggestions; the compiler needs them to understand your instructions.
Every complete statement in Java must end with a semicolon
;. It’s like the period at the end of a sentence. It tells the compiler, "This command is finished."
Curly braces {} are used to group code together into blocks. You can see them around the HelloWorld class and again around the main method. Everything inside the braces belongs to that class or method. This creates a clear hierarchy.
Finally, there's indentation. You'll notice the code inside the main method is pushed in a little. The compiler doesn't care about this, but humans do. Proper indentation makes code vastly easier to read by showing the structure at a glance. It's a critical habit for any programmer.
Writing and Executing
To run a Java program, you first need to save the code in a file. The file name must match the public class name exactly, followed by the .java extension. In our case, the file must be named HelloWorld.java.
Next, you compile the code. Compiling translates your human-readable Java code into Java bytecode, which the machine can understand. You use the Java compiler, javac, for this.
# This command compiles the code
javac HelloWorld.java
If there are no errors, this command creates a new file called HelloWorld.class. This file contains the bytecode. Now, you can run the program using the java command.
# This command runs the compiled program
# Note: you don't include the .class extension
java HelloWorld
The console will then display the output:
Hello, World!
Now you know the basic rhythm of Java development: write, compile, and run. Let's test what you've learned.
What is the primary purpose of the main method in a Java application?
If a Java source file contains a public class named DataProcessor, what must the file be named?
Getting these fundamentals down is the first step. With this structure, you have a scaffold to build any Java application you can imagine.
