Your First Java Program: A Beginner’s Guide to “Hello, World!”
Java is one of the most popular programming languages in the world, known for its simplicity, versatility, and cross-platform capabilities. If you’re new to Java, the best way to start learning is by writing your first Java program: the classic “Hello, World!”. In this guide, we will walk you through writing and understanding your first Java program step by step.
Setting Up Your Java Development Environment
Before writing your first Java program, ensure you have Java installed on your system. If you haven’t installed Java yet, check out our previous article on How to Install Java JDK and Set Up Your Development Environment.
To write and run Java code, you can use:
- An IDE (Integrated Development Environment) like IntelliJ IDEA, Eclipse, or NetBeans.
- A simple text editor like VS Code or Notepad++ combined with a terminal.
Writing Your First Java Program
Let’s create a basic Java program that prints Hello, World! to the console.
Step 1: Create a New Java File
- Open your favorite IDE or text editor.
- Create a new file and name it HelloWorld.java (Java files must have a .java extension).
Step 2: Write the Java Code
Now, type the following Java code into your newly created file:
// This is a simple Java program
public class HelloWorld {
// The main method - Entry point of any Java program
public static void main(String[] args) {
// Prints "Hello, World!" to the console
System.out.println("Hello, World!");
}
}
Step 3: Save the File
Make sure to save the file as HelloWorld.java in a directory where you can easily find it.
Step 4: Compile the Java Program
Before running the Java program, you need to compile it using the Java compiler (javac).
- Open a terminal or command prompt.
- Navigate to the directory where your file is saved using the cd command:
cd path/to/your/file - Compile the Java program using:
javac HelloWorld.java
If there are no errors, the compiler will generate a file named HelloWorld.class.
Step 5: Run the Java Program
Now, execute the compiled Java program using the Java interpreter:
java HelloWorld
You should see the output:
Hello, World!
Congratulations! You have successfully written and executed your first Java program.
Understanding the Java Code Line by Line
Let’s break down the code to understand what each part does:
- public class HelloWorld { – Defines a class named HelloWorld. In Java, every application must have at least one class.
- public static void main(String[] args) { – This is the main method, which serves as the entry point of the Java application. Java programs start executing from this method.
- System.out.println(“Hello, World!”); – This line prints Hello, World! to the console.
- System.out refers to the standard output stream (the console).
- println() is a method that prints the given message and moves to a new line.
Frequently Asked Questions (FAQ)
1. What is the purpose of the main method in Java?
The main method is the entry point of any Java program. It is where execution starts when you run the program.
2. Why does Java use a class to define a program?
Java is an object-oriented programming language, and all code must be written inside a class to maintain structure and reusability.
3. What happens if I forget to include public static void main?
If your program does not include the main method, the Java Virtual Machine (JVM) will not know where to begin execution and will throw an error.
4. What is the difference between print() and println()?
- print() prints text on the same line.
- println() prints text and moves to the next line.
5. Do I need to compile Java programs every time I run them?
Yes, you must compile the program first to convert it into bytecode that the JVM can execute. However, once compiled, you can run the program multiple times without recompiling unless you make changes to the source code.
6. Can I use a different file name than the class name?
No, the file name must match the class name in Java. If your class is named HelloWorld, the file must be saved as HelloWorld.java.
7. What is the difference between javac and java commands?
- javac is the Java compiler that compiles .java files into .class files.
- java is the Java interpreter that executes .class files.
Official Documentation & Java Resources
- Oracle Official Java Documentation – Comprehensive guide on Java programming, including syntax, libraries, and best practices.
- Java SE Downloads – Oracle – Official Java SE JDK download page from Oracle.
- OpenJDK Official Website – Download and learn more about OpenJDK, the open-source implementation of Java.
Java Development Tools
- IntelliJ IDEA – One of the best IDEs for Java development.
- Eclipse IDE for Java Developers – A free, open-source Java IDE.
- Apache NetBeans – Another popular IDE for Java developers.
Conclusion
You’ve successfully written your first Java program and learned how to compile and execute it. This foundational knowledge is essential as you move on to more complex Java concepts.
🚀 Now that you’ve written your first program, let’s explore the main data types in Java in the next article!