Detailed Explanation: Simple Java Program
1. Introduction to Java Program Structure
A Java program is a collection of instructions written in the Java programming language. These instructions
are executed by the Java Virtual Machine (JVM). Java programs are written using classes and methods,
which define the behavior of the program.
2. Structure of a Java Program
Every Java program generally follows this structure:
- Documentation Section (Suggested)
- Package Statement (Optional)
- Import Statements (Optional)
- Interface Statements (Optional)
- Class Definition (Essential)
- main() Method (Essential)
Diagrammatically, the structure looks like:
+----------------------------+
| Documentation Section |
| Package Statement |
| Import Statement |
| Interface Statement |
| Class Definition |
| main() Method |
+----------------------------+
3. Explanation of Each Section
A. Documentation Section:
This section contains comments that describe the program, author details, date, purpose, etc. Comments are
not compiled by the Java compiler. Two types of comments are:
- Single-line: // This is a comment
- Multi-line:
/*
* This is a multi-line comment
*/
B. Package Statement:
This is used to group related classes together into a package. It helps organize large projects.
Syntax: package packagename;
Example: package employee;
Note: This is optional and must be the first statement in the file (excluding comments).
C. Import Statement:
Used to include predefined classes from Java packages.
Example: import java.util.Scanner;
D. Interface Statement:
Defines a blueprint for classes. Optional unless you're using interfaces.
E. Class Definition:
Essential part of a Java program. Contains data members (variables) and methods (functions).
F. Main Method:
Entry point of a Java program.
Syntax: public static void main(String[] args)
Explanation of Keywords:
- public: Makes method accessible to JVM from anywhere.
- static: Method belongs to the class, no object needed.
- void: Method returns no value.
- main: Starting point of the program.
- String[] args: Accepts command-line arguments.
4. Example Program
public class HelloJavaWorld {
public static void main(String[] args) {
System.out.print("Hello");
System.out.println("Java world!");
5. Output Explanation
Output:
HelloJava world!
- System.out.print(): Prints without newline.
- System.out.println(): Prints with newline.
So, "Hello" and "Java world!" appear on the same line.
6. Detailed Breakdown of Each Statement
- public class HelloJavaWorld:
Declares a public class named HelloJavaWorld. The filename must be HelloJavaWorld.java.
- public static void main(String[] args):
This is the method called by the JVM to begin execution.
- System.out.print("Hello"):
Prints the string "Hello" to the screen.
- System.out.println("Java world!"):
Prints "Java world!" and moves to the next line.
- System: A built-in Java class.
- out: Refers to the output stream (screen).
- print()/println(): Methods to display text.
7. Compiling and Running the Program
To compile:
> javac HelloJavaWorld.java
This will generate a file HelloJavaWorld.class containing bytecode.
To run:
> java HelloJavaWorld
This will execute the bytecode using the JVM and display the output on the screen.
8. Summary Table
| Element | Purpose |
|----------------|--------------------------------------------------------|
| Documentation | Comments, for humans only, not compiled |
| Package | Groups related classes |
| Import | Brings in external libraries or Java API |
| Class | Building block of Java code |
| main() | Entry point of program |
| public | Accessible by JVM |
| static | Called without object |
| void | No return value |
| args[] | Holds command-line arguments |
| print() | Outputs on same line |
| println() | Outputs with newline |
| javac | Compiles Java code |
| java | Runs the compiled bytecode |