Introduction
Now that you have set up your Java development environment, it’s time to write and run your first Java program. In this chapter, we will walk you through creating a simple Java program, compiling it, and running it on your computer.
Step 1: Create a New Java File
Choosing a Text Editor or IDE
Before you write your first Java program, you need a text editor or an Integrated Development Environment (IDE) to write your code. Some popular options include:
- Text Editors: Notepad, Sublime Text, Visual Studio Code
- IDEs: IntelliJ IDEA, Eclipse, NetBeans
For this chapter, we will use a simple text editor, but the steps are similar if you choose to use an IDE.
Creating the File
- Open Your Text Editor:
- Open your preferred text editor or IDE.
- Create a New File:
- Create a new file and name it
HelloWorld.java
. The file name must match the class name and should have a.java
extension.
- Create a new file and name it
Step 2: Write the Java Code
In your HelloWorld.java
file, type the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation of the Code
- public class HelloWorld:
- This line declares a public class named
HelloWorld
. In Java, every application must have at least one class definition.
- This line declares a public class named
- public static void main(String[] args):
- This line declares the
main
method, which is the entry point of any Java program. Themain
method is where the program starts execution.
- This line declares the
- System.out.println(“Hello, World!”);
- This line prints the text “Hello, World!” to the console.
System.out
is an output stream used to print text, andprintln
is a method that prints the specified string followed by a new line.
- This line prints the text “Hello, World!” to the console.
Step 3: Compile the Java Program
Before running your Java program, you need to compile it into bytecode that the Java Virtual Machine (JVM) can understand.
- Open Command Prompt or Terminal:
- Open your command prompt (Windows) or terminal (macOS/Linux).
- Navigate to the Directory:
- Use the
cd
command to navigate to the directory where you savedHelloWorld.java
. For example:cd path/to/your/java/file
- Use the
- Compile the Program:
- Type the following command to compile your Java program:
javac HelloWorld.java
- If there are no errors in your code, this command will create a file named
HelloWorld.class
in the same directory.
- Type the following command to compile your Java program:
Step 4: Run the Java Program
Now that your program is compiled, you can run it using the JVM.
- Run the Program:
- In the command prompt or terminal, type the following command:
java HelloWorld
- You should see the output:
Hello, World!
- In the command prompt or terminal, type the following command:
Troubleshooting
- If you see an error saying
javac
orjava
is not recognized:- Make sure you have installed the JDK and set up your environment variables correctly.
- If there are compilation errors:
- Double-check your code for typos and ensure that the file name matches the class name.
Conclusion
In this chapter, you learned how to write, compile, and run a simple Java program. This foundational knowledge is crucial as you begin your journey into Java programming. With your development environment set up and your first program successfully executed, you are ready to explore more complex Java concepts and start building your own applications. Happy coding!