Basic Syntax
This is a basic "Hello World!" program, which is used as an example while learning a new language.
Let us, split the code and understand it line by line.
- public class Main - defines a class named 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 main function shows that it is a public function which means that we can access it anywhere inside of 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.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP
Sign in to track your progress and earn XP!
Exercise 1 of 2Easy
Which of the following is a best practice in programming?
10 XP~2 min
Exercise 2 of 2Easy
Code that is easy to read and understand is called ___ code.
10 XP~2 min