Explanation of First Program in Java

After executing the First Java Program, lets see the explanation in detail.
This is a New Java program.
Call this file “New.java”.
This is a comment. Like most other programming languages, Java lets you enter a remark
into a program’s source file.The contents of a comment are ignored by the compiler. Java supports three styles of comments.
1. The one shown at the top of the program is called a multiline comment. This type of comment must begin with /* and end with */.
2. The second type of comment supported by Java is single-line comment which begins with a // and ends at the end of the line.This is used preferably for line by line comment.
e.g. // Your program begins with a call to main().

Next line,
class New {

This line uses the keyword class to declare that a new class is being defined.
New is an identifier that is the name of the class.

Next line,
public static void main(String args[]) {

  • All Java applications begin execution by calling main( ) function. (This is just like C/C++.)
  • The public keyword is an access specifier, 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. For private, which prevents a member from being used by code defined outside of its class.
  • 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 interpreter before any objects are made.
  • The keyword void simply tells the compiler that main( ) does not return a value.
  • As stated, main( ) is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main.
  • 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. Objects of type String store character strings.
  • The last character on the line is the {. This signals the start of main( )’s body. All of
    the code that comprises a method will occur between the method’s opening curly brace
    and its closing curly brace

Next line,
System.out.println(“This is a New Java program.”);

  • This line outputs the string “This is a New 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.

The first } in the program ends main( ), and the last } ends the Example class

NOTE : When you begin creating applets—Java programs that
are embedded in Web browsers—you won’t use main( ) at all, since the Web browseruses a different means of starting the execution of applets.


What do you think? Please reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s