First Java Program

Welcome to the world of Java programming! Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It's a great language to start with when you're learning to code, and it's powerful enough to build complex applications.

In this tutorial, we'll go through the basics of setting up your Java environment and writing your first Java programs, including the traditional "Hello, World!" example.

Setting Up Your Java Environment

Before you can start writing Java programs, you need to have the Java Development Kit (JDK) installed on your computer. The JDK includes the Java Runtime Environment (JRE), which is needed to run Java applications, as well as the compilers and tools needed to develop them.

  1. Download the JDK: Go to the Oracle website or OpenJDK to download the latest version of the JDK for your operating system.

  2. Install the JDK: Run the installer and follow the instructions to install the JDK on your computer.

  3. Set up the PATH environment variable: Ensure that the JDK's bin directory is in the PATH environment variable so that you can run the Java compiler (javac) and the Java interpreter (java) from the command line.

  4. Verify the installation: Open a command line or terminal and type java -version and javac -version to check that the installation was successful. You should see the version of the Java you installed.

Writing Your First Java Program

Now that you have Java installed, let's write our first Java program.

The "Hello, World!" Program

The "Hello, World!" program is a simple program that displays "Hello, World!" on the screen.

Here's what the "Hello, World!" program looks like in Java:

java
1public class HelloWorld { 2 public static void main(String[] args) { 3 System.out.println("Hello, World!"); 4 } 5}

Let's break down this program:

  • public class HelloWorld: This line declares a class named HelloWorld. In Java, every program must be defined inside a class. By convention, the class name should start with an uppercase letter.

  • public static void main(String[] args): This line defines the main method, which is the entry point of every Java application. The words public and static are modifiers that mean the main method can be called by the runtime without having to instantiate the class. The word void means that this method doesn't return any value. String[] args is an array of strings, which can be used to pass command-line arguments to the program.

  • System.out.println("Hello, World!");: This line is a call to the println method of the System.out object, which prints the string Hello, World! followed by a newline on the console.

Compiling and Running the Program

  1. Save the code: Open a text editor, paste the code above, and save the file as HelloWorld.java. The filename should match the class name and have a .java extension.

  2. Compile the code: Open your command line or terminal, navigate to the directory where you saved HelloWorld.java, and type the following command:

    1javac HelloWorld.java

    This command compiles HelloWorld.java and if there are no errors, it generates a HelloWorld.class file, which is the bytecode that can be run on the Java Virtual Machine (JVM).

  3. Run the program: After successfully compiling the code, you can run the program by typing the following command:

    1java HelloWorld

    You should see the output:

    1Hello, World!

Congratulations! You've just written, compiled, and run your first Java program.

Next Steps

Now that you've learned how to set up your Java environment and write a basic Java program, you can continue learning more about Java's syntax, data types, control structures, classes, and objects. Java is a vast language with many features, but starting with the basics will give you a strong foundation to build upon. Happy coding!