Java Hello World

Java Hello World

For a Learner knowing C/C++, it is not a big task to learn Java as Java is Simple to practice. Just keep 25% efforts of C++ to learn Java. It is enough. Lot of code you write in C/C++ with many iterations involving logic and memory management, forget in Java as predefined methods exist to do the job. This I am proving in every posting in this way2java Site. Be happy that you got an idea to learn Java. I prove all this in this first example "Java Hello World".
Example on Java Hello World that prints Messages at command prompt

Java Hello WorldOutput Screenshot of

Being this Java Hello World is the first example for you, I discuss each line of the code clearly.

We are using 5 keywords of Java in the above code – import, public, class, static and void. System and String are classes. Classes are not keywords in Java. All keywords, all packages and all protocols are lowercase letters. I compare each step here with C for C++ to understand fast as I expect you are from C/C++ background. But a person can learn Java straightaway without the knowledge of these languages also.

import java.lang.*;

import is equivalent to include of C-lang. There we call header file and here we call as package. A header file is equivalent to package in Java. The package name is java.lang. That is, with the above statement, we are importing all the classes of the package java.lang into our class so that we can use them in our coding. Two such classes are String and System. * mark indicates to import all classes.

You can avoid to write this import statement as it is imported implicitly by Java Runtimen Environment. So Programmer never writes it. This is one of the simplicity features of Java.

public class Demo

All the code you write must be enclosed within the braces of the class declaration. Java demands it from you. Here, the class name is Demo. All the code including main() is enclosed within class Demo. public keyword indicates that any other class can access the code of class Demo without any restriction. Infact, public is known as access specifier which species access to other classes.

public static void main(String args[])

I am praising Java for its simplicity but main() method declaration is very big compared to C/C++. But every word you write here has got a good great meaning to compiler. public means the main() method can be accessed by any other code like JVM. JVM stands for Java Virtual Machine, a component of JDK responsible for the execution of a Java program. static is known as access modifier and means that main() can be called without the help of an object by JVM. void means main() method does not return a value. The parameter is a String array used later to access command-line arguments.

System.out.println(“Hello People of the World”);
System.out.println(“Good Morning from way2java Readers”);
System.out.println(“I am great that I got an idea to learn Java”);

System.out.println() is equivalent to printf() of C-lang that prints output at command prompt. Being the first program of yours, I cannot discuss very deeply but I do it briefly. System is a class. out is an object of PrintStream class. println() method is defined in PrintStream class. Any data passed to println() method is printed at command prompt.

Observe, in the above println() methods, \n is not included as \n is added implicitly. Infact, ln in println stands for new line character \n, another simplicity feature of Java.

Compilation and Execution of the above Example with more details of code is given in Java Example Compilation Execution.

Afterwards, how to enter Java and do the programs is given in Java for Beginners.

If you are interested to know more depth of the above code read the following. But I feel it is not the right time to go into now.

1. Access specifiers like public are discussed more elaborately in Access Specifiers Permissions and Restrictions.
2. More notes available at public static void main.
3. Explanation of Access modifiers is given in Access Specifiers Modifiers Java
4. More notes on packages like java.lang at Predefined Packages Java API.
5. To read command-line arguments using String args is given at Command-line Arguments.

Leave a Reply

Your email address will not be published. Required fields are marked *