In this tutorial, you will learn to write "Hello World" program in Java. A "Hello, World!" is a simple program that outputs Hello, World! on the screen. Since it's a very simple program, it's often used to introduce a new programming language to a newbie.
Let 's explore how Java "Hello, World!" program works.
If you want to run this program on your computer, make sure that Java is properly installed. Also, you need an IDE (or a text editor) to write and edit Java code.
// Your First Program
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
If you have copied the exact code, you need to save the file name as HelloWorld.java
. It's because the name of the class and filename should match in Java. Visit this page to learn: Why are filenames in Java the same as the class name?
When you run the program, the output will be:
Hello, World!
// Your First Program
//
is a comment. Comments are intended for users reading the code to better understand the intent and functionality of the program. It is completely ignored by the Java compiler (an application
that translates Java program to Java bytecode that computer can execute). .class HelloWorld { ... }
class HelloWorld { ... .. ... }
public static void main(String[] args) { ... }
public
,
static
, void
, and how methods work? in later chapters.public static void main(String[] args) { ... .. ... }
System.out.println("Hello, World!");
Hello, World!
to standard output (your screen). Notice, this statement is inside the main function, which is inside the class definition.This is a valid Java program that does nothing.
public class HelloWorld { public static void main(String[] args) { // Write your code here } }
Don't worry if you don't understand the meaning of class
, static
, methods, and so on for now. We will discuss it in detail in later chapters.