Java First Program
Every great journey begins with a single step, and in the world of Java, that step is writing and executing your very first program. Before you can build sophisticated applications, it’s essential to understand how a Java program is structured, how it runs, and why the main() method is the gateway for execution. In this chapter, we’ll walk through the process of creating, compiling, and running a Java program, while demystifying the critical role of the main() method.
The Anatomy of Your First Java Program
Output:
Understanding the main () Method
The Anatomy of Your First Java Program
Let’s start by examining a simple Java program. This example prints a welcome message to the console and demonstrates the basic building blocks of Java code.
package quipohouse;
public class FirstProgram { public static void main(String[] args) { System.out.println("Welcome to Quipo House"); }}
Output:
Welcome to Quipo House
Key Components
1. Package Declaration:
package quipohouse;
Organizes classes into namespaces, making code modular and manageable.
2. Class Declaration:
public class FirstProgram { ... }
Every Java program must have at least one class definition.
3. The main() Method:
The entry point for the program. The JVM looks for this method to start execution.
4. Print Statement:
System.out.println("Welcome to Quipo House");
Outputs text to the console.
1. Package Declaration:
package quipohouse;
Organizes classes into namespaces, making code modular and manageable.
2. Class Declaration:
public class FirstProgram { ... }
Every Java program must have at least one class definition.
3. The main() Method:
The entry point for the program. The JVM looks for this method to start execution.
4. Print Statement:
System.out.println("Welcome to Quipo House");
Outputs text to the console.
Understanding the main () Method
The main() method is more than just a starting point; it’s the bridge between your code and the Java Virtual Machine (JVM). Let’s break down its declaration:
public static void main(String[] args)
Explanation of Each Keyword
- public: The method must be accessible to the JVM from outside the class.
- static: Allows the JVM to invoke the method without creating an instance of the class.
- void: Indicates that the method does not return any value.
- main: The name recognized by the JVM as the starting point.
- String[ ] args: An array for command-line arguments, enabling user input at runtime.
Why is the main() Method Important?
The Print Statement Explained
The line:
- Predefined Entry Point: The JVM always starts execution from the main() method.
- Overloading Possible: You can define multiple main() methods with different parameters, but only the standard signature is used as the entry point.
- Command-Line Arguments: Enables dynamic input when running the program.
The Print Statement Explained
The line:
System.out.println("Welcome to Quipo House");
performs a simple yet powerful task-displaying output in the console.
How to Create and Run a Java Program
Let us walk through the practical steps to write, compile, and execute your first Java program.
Run the Program:
You should see the output:
What Happens if There is No main () Method ?
Attempting to run this code will produce and error, as there is no valid entry point for the JVM.
- System: A built-in class containing useful members, including out.
- out: A static member of System class, representing the standard output stream.
- println: A method that prints a message and moves the cursor to a new line.
How to Create and Run a Java Program
Let us walk through the practical steps to write, compile, and execute your first Java program.
Step 1: Write the Program
Open an Editor:
Use Notepad, or an IDE like IntelliJ IDEA, Eclipse, or VS Code.
Type the Code:
Enter your Java code and save the file with a .java extension.
Save the File:
This creates a FirstProgram.class file (bytecode).Open an Editor:
Use Notepad, or an IDE like IntelliJ IDEA, Eclipse, or VS Code.
Type the Code:
Enter your Java code and save the file with a .java extension.
Tip: The filename must match the public class name (e.g., FirstProgram.java).
Save the File:
- Choose “All Files” as the file type.
- Ensure the extension is .java.
Step 2: Compile and Run the Program
Open Command Prompt:
Press Win + R, type cmd, and hit Enter.
Navigate to the File Location:
Use the cd command to change directories to where your Java file is saved.
Compile the Program:
Open Command Prompt:
Press Win + R, type cmd, and hit Enter.
Navigate to the File Location:
Use the cd command to change directories to where your Java file is saved.
Compile the Program:
javac FirstProgram.java
Run the Program:
java FirstProgram
You should see the output:
Welcome to Quipo House
What Happens if There is No main () Method ?
If you try to run a Java program without a main() method, the JVM will not know where to begin execution. This results in a runtime error similar to:
Example:
Error: Main method not found in class MyClass, please define the main method as: public static void main(String[] args)
Example:
public class MyClass { // Not a main method public static void myMethod() { System.out.println("Welcome to Quipo Tutorials!"); }}
Attempting to run this code will produce and error, as there is no valid entry point for the JVM.
Key Point
- Every Java application starts with the main() method.
- The main() method’s signature must be exact for the JVM to recognize it.
- Always save your Java file with the same name as your public class.
- Use System.out.println() for console output.
- Compilation and execution are two separate steps: first compile, then run.
Two Minute Drill
1. Creating a New File:
Open your IDE, create a new project, and add a new Java class
(e.g., HelloWorld.java).
2. Writing the code:
3. Compiling the Program:
Use javac HelloWorld.java to compile.
4. Running the Program:
Use java HelloWorld to execute and see the output.
Open your IDE, create a new project, and add a new Java class
(e.g., HelloWorld.java).
2. Writing the code:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); }}
3. Compiling the Program:
Use javac HelloWorld.java to compile.
4. Running the Program:
Use java HelloWorld to execute and see the output.