Hello World in Java
Let’s create your first Java program! Open your favorite text editor and type the following code:
Save it as HelloWorld.java
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Understanding the Code
public class HelloWorlddeclares a class named HelloWorldpublic static void main(String[] args)is the main method where Java programs startSystem.out.println()prints text to the console
Compiling and Running
Open your terminal or command prompt and navigate to where you saved the file:
# Compile the Java file
javac HelloWorld.java
# Run the compiled program
java HelloWorldℹ️
Notice we run
java HelloWorld without the .java extension!You should see: Hello, World!
Common Issues
Make sure your file name exactly matches your class name. If the class is HelloWorld, the file must be HelloWorld.java.
Learn about variables next to start working with data.
Last updated on