LFPS - Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Hello World

Go down

Hello World Empty Hello World

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

Hello World Program

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

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

Then the main method :
Code:
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.
Ex:
Code:
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.

Code:
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:
Code:
System.out.print("print");
System.out.print("print"+"\n");          // Same as println()
System.out.println("print");

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

Posts : 126

http://osalfps.org

Back to top Go down

Back to top

- Similar topics

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