Hello World Program In Java

We begin our study of Java by understanding the basics like keywords, identifiers and literals. We also study the simple data types viz. integers, floats and characters and the different types of operators supported by Java in next topics.

Let us begin our study of Java by writing a small program to understand how to compile and run a Java program.

CODE/PROGRAM/EXAMPLE
/* A first sample program
this is the way to give multiline comments */
class First {
//Another way of writing comments in the program
public static void main(String args[])
{
System.out.println(“Hello World.!!”);
}
}

In Java, the source file is called a compilation unit. This is a text file which will contain one or more class definitions. The extension to the filename should be .java. Note that this extension is four characters long. All the program code should reside in a class. By convention the name of the class should match the name of the file that holds the program.

This means that the program illustrated above should be stored with a file name First. java.

Also remember that Java is case sensitive. Therefore care has to be exercised while naming the file. To compile the above program execute the Java compiler javac at the command line as shown below:

Syntax
C:> javac First.java

The Java compiler creates a file called First.class. This file contains the bytecode form of the program. This bytecode is interpreted by the interpreter. This means that the output of the java compiler is not a code which can be directly executed.

Hence to actually run the program, you have to use the Java interpreter called java. You can do this by executing the command as shown below :

Syntax
C:> java First

When the program runs you will get the following output :
Hello World.!!

When you execute the Java interpreter as shown above, you are actually supplying the name of the class that you want the interpreter to execute. The interpreter automatically searches for the file by the specified name and which has a .class extension.

Now that you have seen how to successfully compile the program, let us begin our study of the Java programming language. You must note here that those sections where Java has similarities with languages like C and C++ shall be explained in brief and only an overview may be presented. Detailed programs may not be presented for every subtopic.

Also wherever Java differs from these languages will be specifically indicated throughout the course material. Let us study the line :

class First

We use the keyword class to declare that we are defining a new class. First is the name we have given to the class. Thus First is an identifier. The entire class definition, which includes all the members of that class is to be enclosed in a pair of curly braces. This is identical to the way they are used in C and C++.

The next line of code is :

public static void main(String args[])

This line begins the main() method. This is the line where programexecution begins. At this point of time we shall not attempt to study the details of this line. It is enough to note that your program execution begins at this line and that all Java applications begin execution by calling main().

As we progress through our study we shall study this line in detail. It is also important to note that the Java compiler will compile classes that do not contain the main() method, however the Java interpreter cannot run these classes.

The brace bracket indicates the start of the body of the main() method. All the code of a particular method should be enclosed within a pair of braces.

The line -

System.out.println(“Hello World..!!”);

will output
Hello World.!!

on the screen. We use the println() method to output information on the console. In our example, println() outputs a string on the screen. System is a predefined class which provides access to the system and out is the output stream connected to the console.

You should note that console input/output is mostly used for simple, utility programs. Modern computing environments are windowed and graphical in nature, therefore console input/output is not frequently used in real Java programs and applets.

Also note that the println() statement ends with a semicolon (;). All statements in Java must end with a semicolon.

A Java program is a collection of comments, whitespace, identifiers, keywords, literals, separators, and operators. Now that we have studied a simple Java program, let us begin our study of these elements.

#hello_world_program_in_java #first_program_in_java #explain_hello_word_program_in_java

(New page will open, for Comment)

Not yet commented...