How to write hello program in Java

java

Introduction

In this article we are going to learn how to build a “Hello” program using Java, we will write a simple program after installing JDK.

1. Setup Your development Environment

Before start writing program, you have to make sure that you have the right tools installed in your system.

Installing Java Development Kit (JDK)

Before starting the development, you have to install Java Development Kit (JDK), JDK will also have Java Runtime Environment (JRE) and development tools.

  1. Step to download JDK:

  1. Step to Install the JDK:

  • If you have windows, run the installer and follow the steps.
  • For macOS, open the downloaded .dmg file and follow the steps.
  • For Linux, you may need to use a package manager like apt or yum to install the JDK.

Development Environment (IDE)

IDE will help you with tools for writing, debugging and managing code, this tool will help you to streamline the development process.

  1. Choose an IDE:

  1. Install the IDE:

  • Download the IDE from its official website.
  • Follow the installation process specific to your operating system.
  • After installation, launch the IDE and configure it for Java development by setting up a new Java project.

    To ensure that your IDE and java are installed properly, follow the below steps.

  1. Check Java Installation:

  • Open a command prompt (Windows) or terminal (macOS/Linux).
  • Type java -version and javac -version to check if Java and the compiler are installed correctly.
  • You should see version information for both.

  1. Verify IDE Installation:

           Open your IDE and create a new Java project to ensure it’s working as expected.

2. Writing the “Hello” Program

Now your environment is ready, let’s write the “Hello, World!” program.

Creating a New Project

  1. Open Your IDE:

·         Launch your IDE and select the option to create a new project.

  1. Set Up the Project:

·         Follow the prompts to create a new Java project. You might need to name your project (e.g., “HelloProject”).

Writing the Code

  1. Create a New Java Class:

·         In your project, create a new Java class file named HelloWorld. Most IDEs will provide an option to create a new class with a public static void main(String[] args) method.

  1. Enter the Code:
				
					public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello!");
    }
}

				
			

Explanation:

  • public class HelloWorld: Defines a public class named HelloWorld. In Java, every application must have at least one class.
  • public static void main(String[] args): This is the main method where the program starts. It’s public so it can be called from outside the class, static so it can be called without creating an instance of the class, and void means it doesn’t return any value.
  • System.out.println(“Hello”);: This line prints “Hello, World!” to the console.

4. Compiling and Running the Program

Once you’ve written the code, you need to compile and run it.

Compiling the Code

  1. Using the IDE:

·         Most IDEs will automatically compile your code as you save it or provide a build option.

  1. Using Command Line:

·         Open a command prompt or terminal.

·         Navigate to the directory where your HelloWorld.java file is located.

·         Run the command javac HelloWorld.java to compile the code. This will generate a Hello.class file.

Running the Program

  1. Using the IDE:

·         Run your program using the IDE’s run button or menu option.

  1. Using Command Line:

·         In the same directory as your Hello.class file, run the command java HelloWorld.

·         You should see the output: Hello.

Conclusion

Congratulations! You have written, compiled, and run your first Java program. The “Hello” program is a great starting point to learn Java programming for you. From here, you can explore more advanced Java concepts and start building more complex applications.

Additional Resources

To further your Java programming skills, consider exploring the following resources:

I hope you have learned something from this article, Happy codding 😊

Scroll to Top