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!
-
6:02:58
Drew Hernandez
1 day agoGOP COOKED INTO DENIAL & 12 ISRAELI-LINKED PHONES DETECTED AT UVU DAY OF CK EXECUTION?
46.1K22 -
2:46:08
TimcastIRL
7 hours agoFAA To STOP Flights Over Shutdown, May CLOSE Airspace, Thanksgiving Travel APOCALYPSE | Timcast IRL
187K109 -
1:56:20
Tucker Carlson
7 hours agoIt’s Time to Decide: America First or Lindsey Graham’s Psychosexual Death Cult?
108K390 -
9:07:39
SpartakusLIVE
9 hours agoBattlefield 6 - REDSEC || ARC Raiders Later? || Anybody Want Warzone???
70.1K5 -
3:56:11
Alex Zedra
6 hours agoLIVE! Spooky Games tn
47.4K3 -
2:58:21
I_Came_With_Fire_Podcast
17 hours agoThe Normalization of Political Violence | Right Wing In-Fighting | China Chooses China
34.1K10 -
2:37:06
PandaSub2000
12 hours agoLIVE 10:30pm ET | BUZZ TRIVIA with Chat!
35K5 -
1:13:58
ThisIsDeLaCruz
5 hours ago $0.03 earnedWhat Fans Never Hear: Pearl Jam’s Audio Engineer Tells His Story
25.8K5 -
1:39:58
Glenn Greenwald
9 hours agoWhy Did Zohran Win & What Does it Mean? Plus: Dick Cheney, "Hero of the Resistance" | SYSTEM UPDATE #543
137K92 -
4:09:10
This is the Ray Gaming
6 hours ago $0.01 earnedRAYmember RAYmember the 5th of November | Rumble Premium Creator
16.7K7