Understanding Java Program Structure & Execution
Section 1: Learn
Java Program Structure
Every basic Java program has a defined structure. Here's what a simple program
looks like:
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Let’s break it down:
• public class HelloWorld: Declares a class named HelloWorld
• public static void main(String[] args): This is the entry point of every Java
program
• [Link](...): Prints a line to the screen
Why is public static void main(String[] args) Important?
• public: Accessible from anywhere
• static: Doesn’t require object creation to run
• void: Doesn’t return any value
• main: Recognized by JVM as the starting point
• String[] args: Accepts command-line arguments
Printing in Java
You can use:
[Link]("Hello!");
• It prints the text and moves to the next line.
• To print without a newline, use [Link](...).
Importance of Semicolon
• Java is a statement-based language.
• Every statement must end with a semicolon (;).
• Missing semicolon leads to compilation error.
Case Sensitivity
Java is case-sensitive. For example:
• System is correct, but system is wrong.
• Main is not the same as main.
How Java Executes Code Line by Line
The JVM reads your code top to bottom, line by line, executing instructions in
sequence unless told otherwise (e.g., using loops or conditions).
Fun Fact: The famous “Hello World” example is used in almost every
programming language as a first program!
Section 2: Practice
Hello World Example
public class HelloExample {
public static void main(String[] args) {
[Link]("Hello from Java!");
}
}
Try This:
• Change the message to your name.
• Add another [Link](...) to print your college.
Compile and Run
1. Save the file as [Link]
2. Open CMD and navigate to the folder.
3. Run these commands:
javac [Link]
java HelloExample
Output: Hello from Java!
Section 3: Know More (FAQs)
What happens if I forget the semicolon?
You will get a compilation error that halts your program until it's fixed.
Can I change the method name from main to something else?
No. The JVM looks specifically for main. Changing it will cause a runtime
error.
Is Java case-sensitive?
Yes. Main and main are treated as different identifiers.
Why does Java need a class?
Java is class-based. Every method must be inside a class, even the main()
method.
What is the difference between print() and println()?
• print() – stays on the same line
• println() – moves to the next line after printing