Saturday 8 November 2014

Hello World

Let's start with world's former program HelloWorld.
Have a look at the following code.



Save this code in a file and name is as HelloWorld.java
Compile it using the command javac HelloWorld.java. After compiling you will see a HelloWorld.class file in your respective directory. This file contains the java bytecode of our original file which can be readily executed on JVM.
Execute it using the command java HelloWorld
Execute this code and find output. Output will be Hello World!
Let us discuss word by word in the above code
First word class it says that the word following, HelloWorld is a class name and the file which contains this code should be named as its class followed by .java extension in this case HelloWorld.java
Next you can see open brace {, which says that the code inside HelloWorld calls starts now.
Next let us take the whole line public static void main(String args[]). This is the starting point for our code to execute. Each word in this line has its own significance public, static void and main(String args[]). Forget about the first three words for now, we will come for them later.
Now focus on main(String args[]), main() is a method and we are passing a String array as input for this method as String args[].
Next line is System.out.println(“HelloWorld!”); This is a java statement. Every statement in java is terminated by ; Forget about System.out for now, we will come to this later.
println(“HelloWorld!”) is a method which takes any string as an argument and it just prints that string to the console. That is why we are getting output HelloWorld! In the console.
Next you can see two close braces { which closes main() method and HelloWorld class respectively.
Main concept behind this is to make you familiarize with the structure of java program.
If you don't understand the concept here, Don't worry read it again. This is the fundamental for the forthcoming chapters.

No comments:

Post a Comment