Premium Only Content
Java "Hello World"
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!
-
LIVE
PandaSub2000
9 hours agoLIVE 10:30pm ET | BUZZ TRIVIA with Chat!
219 watching -
1:13:58
ThisIsDeLaCruz
2 hours agoWhat Fans Never Hear: Pearl Jam’s Audio Engineer Tells His Story
11K1 -
1:39:58
Glenn Greenwald
6 hours agoWhy Did Zohran Win & What Does it Mean? Plus: Dick Cheney, "Hero of the Resistance" | SYSTEM UPDATE #543
129K79 -
LIVE
This is the Ray Gaming
3 hours agoRAYmember RAYmember the 5th of November | Rumble Premium Creator
69 watching -
1:44:39
vivafrei
5 hours agoArctic Frost is Bigger Scandal than you Think!! Live with Former Green Beret Ivan Raiklin!
58.4K52 -
2:29:57
Turning Point USA
5 hours agoTPUSA Presents This Is the Turning Point Tour LIVE with Eric and Lara Trump at Auburn University!
53.3K13 -
2:27:32
XDDX_HiTower
4 hours agoBATTLEFIELD 6 REDSEC! [RGMT CONTENT Mgr.
8.76K6 -
2:02:56
Joker Effect
3 hours agoSTREAMER NEWS: What actually occurred at Dreamhack ATL. Frontier Airlines. Kick Staff Trolled me?!
6.15K4 -
LIVE
VapinGamers
3 hours agoBF6 RedSpec - Nothing but Wins and New Rum Bot Testing - !rumbot !music
31 watching -
5:55:14
Spartan
8 hours agoStellar Blade for now, also downloaded 20+ games on steam I have yet to play lol
998