Java Hello World!
This post demonstrates how to write a simple Java program.
create file end with
.java
writes the following code
1
2
3
4
5
6public class Hello{
public static void main(String[] args){
System.out.println("Hello World!");
}
}run
javac filename.java
to compile. If successful, a new file namefilename.class
will be created.finally, run
java filename
to run the program.
Some elaboration:
public
is an access modifier indicating that the class and method can be accessed by others.static
means it's a static method that is accessible without creating an instance of the class.void
means the method doesn't return anything.main
is the entry point for any Java program. When the program start, it looks for the main method and executes it.System.out.println()
is a built-in method call.Hello World!
is a string literal.- Java can be considered as both interpreted and compiled language. It
compiles the source code into
.class
files when you runjavac
, and JVM interprets the compiled code when you runjava
.