Basic Syntax:
helloworld.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
This is a basic program which prints "Hello World!" on screen.
Let us, split the code and understand it line by line.
- public class Main - defines a class names Main which is public. In Java, we have to work with classes. The Main class is the entry point for our program.
- public static void main(String[] args) - it is a special method inside main class. The execution of our code starts from main method.
- public : access modifier written before the main function shows that it is a public function which means that we can access this function anywhere within our java program.
- static : indicated that the method belongs to class itself.
- void : means that the function is not returning any value.
- main : name of the method.
- String[] args :This is the parameter to the main function which allows us to pass command line argumentsto our program when we run it.
- System.out.println("Hello World"); : This is the actual line of code which is getting executed.
- System : buil-in class in Java.
- out : static field with System class which represents standard output stream.
- println("Hello World"); : This is the method call on out object. It prints text "Hello World!" on screen.
Note : We have to take care about semi-colon. We have to put semi-colon at the end of every expression. If you do not put semi-colon at the end of every expression, you are going to get an error.