Hello World
A simple Hello world program with just the main() method. main() method is an entry point for any java program i.e execution starts with the main method.
Main method has a predefined signature if not followed will lead to errors.public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World !"); } }Lets understand the keywords used in the HelloWorld program:
- public : public is the access specifier that makes the method public so that the method can be invoked outside of this file/package/project otherwise JVM will not be able to invoke main method.
- static : static is the access modifier that marks the main method a class level member. As the main method is static, it can be invoked with class name without creating any instance of the class.
- void : void marks the main method as void that it’s not going to return any value.
Compiling & Running the program from Terminal:
- Go to command prompt/Terminal
- Move inside the folder containing the program code
- Use JAVAC compiler to compile into .class file
JAVAC
e.g
JAVAC HelloWorld.java
Output
HelloWorld.class
- Use JAVA command to run .class file
JAVA
e.g
JAVA HelloWorld