Writing and Executing Your First Java Program: A Step-by-Step Guide
Introduction
In this section, we will learn how to write and execute our very first Java program. We will cover
the following topics:
Setting up the Java development environment
Understanding the basic anatomy of a Java program
Writing and executing the "Hello World" program
Setting up the Java Development Environment
Before we can start writing Java programs, we need to install the Java Development Kit (JDK)
and set up the environment. Here are the steps:
Download the JDK installer from the official Oracle website.
Run the installer and follow the on-screen instructions to install the JDK.
Set the environment variable JAVA_HOME to the installation directory of the JDK.
Add the bin directory of the JDK to the PATH environment variable.
Understanding the Basic Anatomy of a Java Program
A Java program consists of the following basic components:
•Classes: A class is a blueprint for creating objects in Java. It can contain fields, methods, and
constructors.
•Methods: A method is a code block that performs a specific task. It can take input parameters
and return a value.
•Variables: A variable is a named storage location that can hold a value. It has a type, a name,
and a value.
Writing and Executing Your First Java Program: A Step-by-Step Guide
Introduction
In this section, we will learn how to write and execute our very first Java program. We will cover
the following topics:
Setting up the Java development environment
Understanding the basic anatomy of a Java program
Writing and executing the "Hello World" program
Setting up the Java Development Environment
Before we can start writing Java programs, we need to install the Java Development Kit (JDK)
and set up the environment. Here are the steps:
Download the JDK installer from the official Oracle website.
Run the installer and follow the on-screen instructions to install the JDK.
Set the environment variable JAVA_HOME to the installation directory of the JDK.
Add the bin directory of the JDK to the PATH environment variable.
Understanding the Basic Anatomy of a Java Program
A Java program consists of the following basic components:
Classes: A class is a blueprint for creating objects in Java. It can contain fields, methods, and
constructors.
Methods: A method is a code block that performs a specific task. It can take input parameters
and return a value.
Variables: A variable is a named storage location that can hold a value. It has a type, a name,
and a value.
Hello World Program
Now that we have set up the Java development environment and understand the basic
components of a Java program, let's write our first Java program: the "Hello World" program.
Open a text editor and create a new file named HelloWorld.java.
Add the following code to the file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Save the file and exit the text editor.
Open a command prompt or terminal window and navigate to the directory where the
HelloWorld.java file is located.
Compile the Java program by running the following command:
javac HelloWorld.java
Execute the Java program by running the following command:
java HelloWorld
The program should print the following output:
Hello World!
Explanation
Let's break down the code of the "Hello World" program:
•public class HelloWorld: This line declares a public class named HelloWorld. A class is a
blueprint for creating objects.
•public static void main(String[] args): This line declares the main method. The main method is
the entry point of a Java program.
•System.out.println("Hello World!");: This line prints the string "Hello World!" to the console.
Congratulations! You have successfully written and executed your first Java program!