Previous | Home | Next |
/* This is a sample java program Save this file as Hello.java */ class Hello { // A java program will start from here. public static void main(String args[]) { System.out.println("Hello Delhi "); }}
output :
Hello Delhi
Suppose if you are write your program in notepad, then save this file as Hello.java
Compiling the program:After we have written our program we need to compile and run the program. For that we need to use the compiler called javac which is provided by java. Go to the command prompt and type the file name.
c:\>javac Hello.java
The javac compiler will create a class file called Hello.class that contains only bytecodes. These bytecodes have to be interpreted by a Java Virtual Machine(JVM) that will convert the bytecodes into machine codes. Once we successfully compiled the program, we need to run the program in order to get the output. So this can be done by the java interpreter called java.
c:\>java Hello
The output will be displayed as
Hello Delhi
Looking into the program line by line
/*This is a sample java program Save this file as Hello.java*/
This is called comment. This is for us to enter the comments about the program for our own convenience. The contents of a comment will be ignored by the compiler. Actually java supports three styles of comments. The above one is called multi-line comment which may contain several lines. This type of comment must begin with /* and end with */.
The next line of the code in a program is
class Welcome {
The word class is a keyword to define a new class and Hello is a name of the class. The class definition must begins with opening curly brace ({) and ends with closing curly brace (}). The rest of the things defined inside these braces are called member of the class. And note that all the program activities are defined inside the class.
// A java program will start from here.
This is another type of comment. This is called single line comment starts with // and ends with end of the line.
The next line of the code in a program is
public static void main(String args[])
This line begins with main method as like functions. The program will start execute by calling this main method. The keyword public is an access specifier. The keyword static is a kind of modifier. The keyword void means that the method main() does not return any value. As we had seen before all the java program will start execute by calling the main method. If we want to pass any information to a method will be received by the variables declared within the parenthesis is called parameters. In a main() method there is only one parameter ,String args[] . args[] is a name of the parameter that is an array of the objects of data type String. String store sequences of characters and args will receive the command line arguments.
All the method in java must be start with opening curly brace ({) and ends with closing curly brace (}).
The next line of the program is
System.out.println("Hello Delhi");
This System.out.println helps to display the output in the command line. The System.out.println statement ends with ; (semicolon). All statements in java must end with semicolon. And remember that java is case sensitive. So we should be very careful about cases while coding the program. Otherwise it will lead to the serious problems.
Previous | Home | Next |