"Hello, world!" in Java

The "Hello, World!" program is a traditional first program when learning a new programming language. In this tutorial, we will guide you through writing and executing a "Hello, World!" program in Java. Java is a versatile and platform-independent programming language widely used for various applications. We will provide step-by-step instructions for executing the program on Windows, Linux, and Mac operating systems.

An introduction to Java

Java is a widely used, high-level programming language that was developed by Sun Microsystems (now owned by Oracle Corporation) in the mid-1990s. It was designed to be platform-independent, meaning that Java programs can run on any operating system or device that has a Java Virtual Machine (JVM) installed.

One of the key features of Java is its "write once, run anywhere" principle. This means that once a Java program is compiled into bytecode, it can be executed on any system with a compatible JVM, regardless of the underlying hardware and software. This portability has made Java popular in a variety of domains, including web development, enterprise software, mobile applications, and more.

Java is an object-oriented programming language, which means it organizes code into objects that interact with each other. It supports the key principles of object-oriented programming, such as encapsulation, inheritance, and polymorphism. Java's object-oriented nature makes it a powerful and flexible language for building complex applications.

The Java Development Kit (JDK) provides tools for developing, debugging, and testing Java programs. It includes a compiler that translates Java source code into bytecode, and a wide range of libraries and APIs (Application Programming Interfaces) that simplify common programming tasks. The JDK also includes the Java Runtime Environment (JRE), which is necessary for executing Java programs.

Java has a rich ecosystem of frameworks and libraries that enhance its capabilities and make development more efficient. Popular frameworks like Spring and Hibernate provide solutions for building web applications and managing databases, respectively. Additionally, Java has a vast community of developers, which means there are numerous resources, tutorials, and support available for learning and troubleshooting.

In recent years, Java has evolved to keep up with modern programming trends. It has introduced new features and enhancements in its latest versions, including lambdas, streams, modules, and improved performance. Java also has strong support for multi-threading, enabling developers to write concurrent and scalable applications.

Overall, Java's combination of platform independence, object-oriented programming, robust tooling, and extensive library support has made it a popular choice for building a wide range of applications across different industries.

Prerequisites

Before starting this tutorial, ensure that you have Java Development Kit (JDK) installed on your computer. You can download the latest version of JDK from the Oracle website and follow the installation instructions specific to your operating system.

Step 1: Writing the Java Program

Open a text editor (such as Notepad, Sublime Text, or Visual Studio Code) and create a new file. In this file, enter the following code:

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

This code defines a class named HelloWorld with a main method. The main method is the entry point for executing the Java program. Inside the main method, we use the System.out.println statement to print the message "Hello, World!" to the console.

Step 2: Saving the Java Program

Save the file with a .java extension, such as HelloWorld.java. Make sure to remember the location where you saved the file for future reference.

Step 3: Compiling the Java Program

Open a terminal or command prompt to compile the Java program. Navigate to the directory where you saved the HelloWorld.java file using the cd command. Type the following command:

javac HelloWorld.java

This command uses the javac compiler included in the JDK to compile the Java source code. If there are no errors in the code, a new file named HelloWorld.class will be generated in the same directory.

Step 4: Executing the Java Program

Now, let's run the compiled Java program with the following command:

java HelloWorld

Congratulations! You have successfully written and executed your "Hello, World!" program in Java.

Conclusion

In this tutorial, you learned how to write, compile, and execute a "Hello, World!" program in Java. Java is a versatile programming language that allows you to build a wide range of applications. Understanding the basics of Java programming is essential for further exploration and development in the Java ecosystem.

Explore other "Hello, World!" examples