Hello World in Java
A "Hello, World!" program is a very basic computer program that simply outputs the message "Hello, World!" (or a similar message) on the screen. It's typically the first program someone writes when learning a new programming language.
Classic
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello, World!");
}
}
Hello, World!
This how a Hello World program is being written in Java. If you are wondering about what these terms class, public static means then don't worry. We will be covering them in detail. In recent updates Java has made some changes making it easier for beginners.
- In Java, every application starts with a class name that must match the filename.
- Let's create our first Java file, called HelloWorld.java. This can be done in any IDE.
- In Java, every line of code must be inside a class. In our example, we named the class HelloWorld.
- Java class names must start with a capital letter.
Note: Java is a case-sensitive language. This means that HelloWorld and helloWorld are considered to be different.
Using Record
Another alternative is to use record instead of classes.
record HelloWorld(){
public static void main(String[] args){
System.out.println("Hello, World!");
}
}
Java 21
In Java 21 they have this feature called unnamed class which makes the same program look like this.
void main() {
System.out.println("Hello, World!");
}
However, this feature is still in preview and needs to be enabled explicitly.