Java “Hello World” program- Demystified

public class Example
{
	public static void main (String args[])
	{
		System.out.println(Hello);
	}
}

Before explaining the above program, let’s first learn about few rules which you should be aware about while writing any Java program:

  • Name of the Java program file should be similar to the name of the class in which “public static void main(String args[])” method is used.
  • Example– for the above program, the name of the java file should be “Example.java” (remember it’s case sensitive)
  • METHOD: For every method name, the first word, first letter should be small and next word, first letter should be in Caps.
  • For example– myShow(), input(), employeInput()
  • CLASS: For every Class name, every word, first letter should be in Caps.
  • For example– UseSystem, Automobile, Student
  • Like in C++ we used to write
COUT<< "hello world";

to get output on screen

  •  Here we write in java, to get output on the screen
     System.out.println(Hello);
  • In any Identifier there should not be any space, special character (except $ or _) and first character should not be a number. (Identifiers are used for class names, variables names method names).

Program Demystified

  1. Main– name of the method
  2. Void– data type of the method (as this method doesn’t return any value, hence it’s VOID)
  3. Static– Basically whenever we have to access a method of a class, we have to create an object of that class.
For example: 
class Rectangle
 {
             int length;
             int breadth;
                   public void setData(int a,int b)
                  {
                                length=a;
                                breadth=b;
                    } 
                public  getData()
                 {
                              System.out.println("length="+length);
                           System.out.println("length="+breadth);
                }
      }

Now in case we want to access above methods, void setData() and getData(). Then we have to create an object first:

RECTANGLE r1;

and then only we can access that method by using Dot operator (object_name.method_name)

r1.getData();

But what happens by using keyword “STATIC” is, it allows a particular method to be called without having to instantiate a particular of that class (means by using it, we can access same method without making an object of that class).

  • So in above program, we are accessing “Example” Class, “public static void main (String args[])” , method without making any object of the class. Just because we have made the above method, “STATIC”.

4.  System: It’s an predefined class
5.  Out: It’s an object of “printStream” class (we will study in detail, about this class in coming posts)
6.  Println: It’s a method of “printStream” class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s