Friday, February 9, 2018

A First Simple HelloWorld Program In Java

In this post i have explain the simple HelloWorld program in java .
/*
This is a simple Java program.
Call this file "HelloWorld.java".
*/
class HelloWorld {
// Your program begins with a call to main().
        public static void main(String args[]) {
                   System.out.println("This is a simple HelloWorld Java program.");
         }
}


Entering The Program :-

For most computer languages, the name of the file that holds the source code to a program is immaterial. However,this is not case of Java. The first thing you must learn about java is that the name you give to a source file is very important. For this example ,the name of the source file should be HelloWorld.java

Let's see why..


In Java, a source file is official called a compilation unit. it is a text file that contains one or more class definitions.(For now,we will be using source file that contain only one class)
The Java compiler requires that a source file use the .java file name extension.

As you can see by looking at the program,the name of the class defined by the program is also HelloWorld. This is not a coincidence.In Java, all code must be  inside a class.
By convention,the name of the main class should match the name of the file that holds the program. You should also make sure that the capitalization of the file name matches the class name.
The reason for this is that java is case-sensitive.At this point,the convention that file names correspond to class name may seem arbitrary.However,this contention makes it easier to maintain
and organize your programs.

Compiling the program :-

To compile the HelloWorld program,execute the compiler,javac,specifying the name of the source file on the command line, as shown here :
c:\>javac HelloWorld.java

The javac compiler creates a file called HelloWorld.class that contains the bytecode version of the program. The java bytecode is the intermediate representation of your programs that contains instructions the Java Virtual Machine will execute.

To actually run the program,you must use the Java application launcher called java. To do so,pass class name HelloWorld as a command-line argument, as shown here:
c:\> java HelloWorld

When the program is run the following output displayed:A First Simple Program

This is a simple HelloWorld Java program.

A Closer Look at the First Sample Program:-


Although HelloWorld.java is quite short, it includes several key features that are common to all Java programs. Let's closely examine each part of the program.
The program begin with the follwing lines:
/*
This is a simple Java program.
Call this file "HelloWorld.java".
*/
This is comment,Like most other programming languages.Java lets you enter a remark into a program's source file.The contents of comment are ignored by the compiler.

The next line of code in the program is shown here:


class HelloWorld {
 
This line uses the keyword class to declare that a new class is being defined.HelloWorld is an identifier that is the name of the class. The entire class definition,including all of its members,will be between opening curly brace ({) and closing curly brace (}).

The next line in the program is the single-line comment, shown here:


// Your program begins with a call to main().

This is single line comment-> A single-line comment begins with a // and ends at the end of the line

The next of code is shown here :


public static void main(String args[]) {

This line begins the main method. All Java applications begin execution by calling main( ).

The public keyword is an access modifier,which allows the programmer to control the visibility of class members.When a class member is preceded by public,then that member may be accessed by code outside the class in which it is declared.(The opposite of public is private, which prevents a member from being used by code defined outside of its class.)
In this case main must be declared as a public, since it must be called by code outside of its class when the program is started.
The keyword static allows main() to be called without having to instantiate a particular instance of the class.This is necessary since main() is called by the Java Virtual Machine before any objects are made.The keyword void simply tells to compiler the main does not return a value.

Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, you still need to include the empty parentheses. In main( ), there is only one parameter, albeit a complicated one.

String args[ ] declares a parameter named args, which is an array of instances of the class String. (Arrays are collections of similar objects.) Objects of type String store character
strings. In this case, args receives any command-line arguments present when the program is executed.

The next line of code is shown here. Notice that it occurs inside main().

System.out.println("This is a simple HelloWorld Java program.");

This line outputs the string "This is a simple HelloWorld Java program." followed by a new line on the screen. Output is actually accomplished by the built-in println( ) method. In this case, println( )
displays the string which is passed to it.

As you will see, println( ) can be used to display other types of information, too.
The line begins with System.out.

System is a predefined class that provides access to the system, and out is the output stream that is connected to the console.

Data Types in java


No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...