Hello World

View previous topic View next topic Go down

Hello World

Post by Sujith on Sun Aug 01, 2010 5:26 am

Hello World Program

class Hello {
    public static void main(String[] args) {

This Tutorial will explain to you how this simple program works. To start off a class named Hello is declared.
class Hello {}

Then the main method :
public static void main(String[] args) {}

If you don't know what the 'public' keyword does then read my other tutorial on ACCESS SPECIFIERS.

Moving on to the 'static' keyword - it makes a variable or method a class variable or method. Class variables and methods can be accessed by all methods, constructors in that class.
class StaticTest {
    static int rollNo=25;
    //GetNumber Method
    public static int getNumber() {
        return rollNo;              //class variable accessible here
    //Main Method
    public static void main(String[] args){
        int r=getNumber();      //class method accessible here

Then there is the return type. The main method is basically just a function so it has to have a return-type. The 'void' type suggests the method returns nothing.

'main' is the name of the function, this always has to be main for the main method, because the compiler recognizes 'main' as a keyword.

Anything that comes in the curved brackets are called Arguments or Parameters.

Parameter: It is a variable that can be sent to a method.

Argument: It is a value that can be sent to a method.

int a=25;
math.sqrt(a);          // Parameter
math.sqrt(25);        // Argument

In the main method the is a parameter that receives command-line arguments from the prompt. You can give it any name you want, it doesn't always have to be args.

Finally two print statements. To differentiate between print() and println() here's a simple example:
System.out.print("print"+"\n");          // Same as println()

So basically the println() method of the out class just adds a line after the sentence.
This concludes the Hello World program tutorial. Smile

It is the simplest things that are most Complicated!
Been on this forum since 2608 days!

LSF Expert
LSF Expert

Posts : 126


Back to top Go down

View previous topic View next topic Back to top

Permissions in this forum:
You cannot reply to topics in this forum