Your path to becoming an Ethical Hacker! Hacking Academy Try It Now!

Java Hello World Program

Learn how to create, compile, and run a simple Java program. This guide breaks down each step, from writing code to executing it on your terminal.

Java stands out as one of the most popular and versatile programming languages today. Known for its speed, reliability, and security, Java is utilized in a variety of applications, ranging from desktop and web applications to scientific supercomputers, gaming consoles, and mobile devices. In this guide, we'll explore how to write a basic Java program, providing a foundation for your journey into Java programming.

Java Hello World Program

Steps to Implement a Java Program

To implement a Java application, follow these steps:


1. Creating a Java Program

You can write a Java program using a text editor like Notepad or an Integrated Development Environment (IDE) such as NetBeans. Here's a simple example:

class Test {

    public static void main(String[] args) {

        System.out.println("My First Java Program.");

    }

}

Save this file as Test.java.


2. Compiling the Java Program

To compile your program, open the command prompt and run the Java compiler (javac) with the name of your source file:

javac Test.java

If there are no errors, the javac compiler will create a file called Test.class that contains the bytecode of your program.


3. Running the Java Program

To run your compiled program, use the Java interpreter by typing the following command in the terminal:

java Test


The following program is a basic Java example that prints "Hello World" to the screen. Let's break down the code step by step to understand each part.

// This is a simple Java program.

// FileName : "HelloWorld.java".

 

class HelloWorld {

    // Your program begins with a call to main().

    // Prints "Hello, World" to the terminal window.

    public static void main(String[] args) {

        System.out.println("Hello, World");

    }

}

Output:

Hello, World


Understanding the Code

Class Definition

The line class HelloWorld { declares a new class named HelloWorld. All the code for this class, including its methods and variables, will be contained within the curly braces { }.


HelloWorld Identifier

HelloWorld is the name of the class. Everything defined for this class is enclosed within the opening { and closing } braces.


The main Method

In Java, every application must have a main method. This method is the entry point of the program and has the following signature:

public static void main(String[] args)

Explanation:

  • public: Allows the JVM to execute the method from anywhere.
  • static: The method can be called without creating an instance of the class.
  • void: The method does not return any value.
  • main: This is the name of the method that JVM looks for as the starting point of the application.
  • String[] args: The method accepts a single argument, which is an array of String elements.


Printing to the Terminal

Inside the main method, the following line is executed:

System.out.println("Hello, World");

This line prints the string "Hello, World" followed by a new line to the terminal. Here’s how it works:

  • System is a predefined class that provides access to the system.
  • out is an output stream connected to the console.
  • println is a method of the out object that prints the argument passed to it.


Comments in Java

Comments can be single-line or multi-line:

  • Single-line comments start with //.
  • Multi-line comments start with /* and end with */.


Example:

// This is a single-line comment.

/* This is a 

   multi-line comment. */

Important Points

  • The class name HelloWorld matches the filename HelloWorld.java. In Java, the public class name must match the filename.
  • By convention, the name of the class containing the main method should match the filename.


Compiling and Running the Program

To compile and run the program, follow these steps:

1. Compile the Program:

Open a terminal and navigate to the directory containing HelloWorld.java. Compile the program using the javac compiler:

javac HelloWorld.java

This creates a HelloWorld.class file containing the bytecode.


2. Run the Program:

Execute the compiled bytecode using the JVM:

java HelloWorld

This command will print "Hello, World" to the terminal.


In Windows

Java Hello World Program

In Linux

Java Hello World Program

إرسال تعليق

Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.