Java "Hello World"

1 month ago
9

Java Tutorial 1: Printing "Hello, World!"

This tutorial covers the very first step in Java programming: printing "Hello, World!" on the screen. This simple program introduces the structure of a Java program and demonstrates how to display text in the console.
Writing the Code:

In your HelloWorld.java file, type the following code:

java
Copy code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Understanding the Code:

public class HelloWorld: Defines a class named HelloWorld. In Java, every program must have at least one class, and the file name must match the class name.
public static void main(String[] args): This is the main method, the entry point of every Java application. When you run the program, this method is executed.
System.out.println("Hello, World!");: This line prints "Hello, World!" to the console. The System.out.println method outputs text to the screen, followed by a new line.
Compiling and Running the Program:

Compile the code by opening a terminal, navigating to the folder where HelloWorld.java is saved, and running:

bash
Copy code
javac HelloWorld.java
This command compiles the code and creates a file named HelloWorld.class.

Run the program with:

bash
Copy code
java HelloWorld
You should see the output:

Copy code
Hello, World!Writing the Code:

In your HelloWorld.java file, type the following code:

java
Copy code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Understanding the Code:

public class HelloWorld: Defines a class named HelloWorld. In Java, every program must have at least one class, and the file name must match the class name.
public static void main(String[] args): This is the main method, the entry point of every Java application. When you run the program, this method is executed.
System.out.println("Hello, World!");: This line prints "Hello, World!" to the console. The System.out.println method outputs text to the screen, followed by a new line.
Compiling and Running the Program:

Compile the code by opening a terminal, navigating to the folder where HelloWorld.java is saved, and running:

bash
Copy code
javac HelloWorld.java
This command compiles the code and creates a file named HelloWorld.class.

Run the program with:

bash
Copy code
java HelloWorld
You should see the output:

Copy code
Hello, World!

Loading 1 comment...