Program Structure in Java
• Components of a Java Program:
• Package Declaration (optional)
• Import Statements (optional)
• Class Definition
• main Method
• Key Characteristics:
• Java is case-sensitive.
• Code is organized into classes and methods.
• Program execution starts from the main method.
General Structure of a Java
Program
• General Structure of a Java Program
• Code Template:
• // Package declaration (optional)
• package myPackage;
• // Import statements (optional)
• import [Link];
• // Class declaration
• public class MyProgram
• {
• // Fields (variables)
• // Methods
• public static void main(String[] args)
• {
• // Statements
• [Link]("Hello, World!");
• }
• }
General Structure of a Java Program
• public class HelloWorld
• {
• public static void main(String[] args)
• { [Link]("Hello, World!");
• }
• }
• Explanation:
• The class name is HelloWorld.
• The main method prints "Hello, World!" to the
console.
General Structure of a Java Program
• Execution Steps:
• Write the program in a .java file.
• Compile the program using the javac
command:
Example: javac [Link]
Run the compiled program using the java
command:
Example: java HelloWorld
Output: Hello, World!
General Structure of a java Program
• Key Points to Remember
• Class Name: Should match the filename (case-
sensitive).
• Curly Braces {}: Define blocks of code.
• Semicolons ;: Terminate statements.
• Comments:
• Single-line: // Comment
• Multi-line: /* Comment */