Java Hello World!
This post demonstrates how to write a simple Java program.
create file end with
.javawrites 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.javato compile. If successful, a new file namefilename.classwill be created.finally, run
java filenameto run the program.
Some elaboration:
publicis an access modifier indicating that the class and method can be accessed by others.staticmeans it's a static method that is accessible without creating an instance of the class.voidmeans the method doesn't return anything.mainis 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
.classfiles when you runjavac, and JVM interprets the compiled code when you runjava.