Hello World
Page 1 of 1
Hello World
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.
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Feb 14, 2012 9:02 pm by Sujith
» Kolaveri Di
Tue Nov 22, 2011 5:11 pm by Sujith
» Plane of Remembrance
Mon May 23, 2011 7:44 am by Sujith
» Board Exam Results 2011
Fri May 13, 2011 10:19 pm by Sujith
» Source Code
Wed May 04, 2011 8:06 pm by Sujith
» Rio
Wed May 04, 2011 8:04 pm by Sujith
» Fast and Furious 5
Wed May 04, 2011 7:56 pm by Sujith