Here we will write Hello World in java and try to understand basics of how it works.
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World");
}
}
- How to run?
- Save the above code in a file with name – HelloWorld.java
- if you are using an IDE like Eclipse/Intellij , right click on file and click run
- or from command line
- first run
javac HelloWorld.java
- then run
java HelloWorld
- first run
- Now Let’s understand
- First of all we have described a class here with Name HelloWorld
public class HelloWorld
- In this we described a function with name
main
aspublic static void main
- Main function with this signature is special to java as it calls this main function when your program runs.
- Now in this main function we have written
System.out.println("Hello World");
- With this line we are telling the System to print the text passed(which is “Hello World” in this case) and system prints this on the command line.
- First of all we have described a class here with Name HelloWorld