Thursday, May 12, 2011

Hello World !!

Hola,

Its almost 2+ years.. and I wrote a program of Java

Program which is very first in any standard book, for those who are very new to java..or any programing language in that sense.. its a "Hello World"..


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

here, are some short cuts those I still remember

1. type "sysout" +and [ctr+space] it will become public static void main
2. Write a class\Package and [Cts+Space] it will display you sub-classes\classes\methods \variables inside that.
3. Ctr+H , which will be useful for searching file or class - it will open a new dialog box.
4. Ctr+L, you will get an option to enter line number on which you wants to move
5. Eclipse will show an error in a program by highlighting the erroneous line and show one red square at left most side. If you double click on the square, it will suggest you the options to correct the code - which will be very helpful to write a code.
.....

In above example we have called the SOP statement inside PSVM method itself. PSVM is mandatory method in any Java program, we will discuss more on this in some days..


public class MyWorld {

void firstMethod(){
System.out.println("Hello World");
}
public static void main(String args[]){
new MyWorld().firstMethod();
}
}

In above program, we have created another method firstMethod() which return nothing - so void . We have called this method from PSVM. As we can not call method - unless it is static - directly, we have to create an object of class. Reference [ a.k.a. Pointer] of that object will point to memory location where this method is reside and will display the result.

Here, Object and Class are 2 different memory locations and because of which you can not call or refer resources belongs to class from object or vice verse.

e.g. void firstMethod() is a method belongs to object and can not be called directly from class. You have to first create an object of that class and after that only you can refer to the method.

---- See below program

public class MyWorld {

static void firstMethod(){
System.out.println("Hello World");
}
public static void main(String args[]){
firstMethod();
MyWorld.firstMethod();

}
}


Here, our method static void firstMethod() is explicitly termed as static, which means this method belongs only to class and not of object. This method can be called from class - no object reference is required. Here, there are 2 calls

firstMethod();
MyWorld.firstMethod();

second one is preceded by Class Name whereas first one is not. when we do not mention the class name, java will consider it as first parent class - so its not necessary to mention it.

We will see in more detail regarding the significance of Public, Static, Void, Main and input String args[] in subsequent sessions.

Good bye till then

1 comment:

  1. I will be referring SCJP 1.4 book by Khalid Mughal. Reason is very simple, I have only that book with me, which I have studied earlier and feel very good among others.

    Though new Java versions are available and 1.4 is no more used, but I think basic OPPS concepts will not change in any java version which are also same in C++ and at some extent Oracle.

    ReplyDelete

All about CSS

From book HTML & CSS - Design and Build Websites - Jon Duckett CSS works by associating rules with HTML elements. These rules govern how...