Looking at First Java program :
This is demonstrating simple example program that displays the text,” My First JAVA program...............” on the console.
public class FirstProg { //This is a first simple java program. public static void main(String[] args) { System.out.println("My First JAVA program..............."); } }
Explanation :
public :
A keyword of the Java language that indicates that the element that follows should be made available to other Java elements.
As a result, this keyword indicates that the FirstProg class is a public class, which means other classes can use it.
class :
Another Java keyword that indicates that the element being defined here is a class. All Java programs are made up of one or more classes.
A class definition contains code that defines the behavior of the objects created and used by the program.
FirstProg :
An identifier that provides the name for the class being defined here. While keywords, such as public and class, are words that are defined by the Java programming language, identifiers are words that you create to provide names for various elements you use in your program.
In this case, the identifier FirstProg provides a name for the public class being defined here.
{ :
The opening brace on line 2 marks the beginning of the body of the class. The end of the body is marked by the closing brace on line 7.
Everything that appears within these braces belongs to the class. As you work with Java, you’ll find that it uses these braces a lot.
//This is a first simple java program. :
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. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code.
In this case, the comment describes the program and reminds you that the source file should be called FirstProg.java. Of course, in real applications, comments generally explain how some part of the program works or what a specific feature does.
Java supports three styles of comments. The one shown at this program is called a single-line comment. This type of comment must begin with // as shown in the program.
The second style of the comment is multiline comment. This type of comment must begin with /* and end with */. Anything between these two comment symbols is ignored by the compiler. As the name suggests, a multiline comment may be several lines long.
For example :
/*This is a first
simple java program. */
The third style of the comment is documentation comment. This type of comment is used to produce an HTML file that documents your program.
The documentation comment begins with a /** and ends with a */. Documentation comments are explained in next chapter.
public :
The public keyword is used again, this time to indicate that a method being declared here should have public access.
That means classes other than the FirstProg class can use it. All Java programs must have at least one class that declares a public method named main.
The main method contains the statements that are executed when you run the program.
static :
If you are want to execute any elements (properties) without an object means before the object creating then it must declare a static so compiler directly execute those static elements.
Here the main method is executed before the any object is creating so it must declare a static. 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.
void :
In Java, a method is a unit of code that can calculate and return a value.
For example :
you could create a method that adds two numbers. Then, the addition would be the return value of the method.
If a method doesn’t need to return a value, you must use the void keyword to indicate that no value is returned. Because Java requires that the main method not return a value, you must specify void when you declare the main method.
main :
Finally, the identifier that provides the name for this method.
Java requires that this method be named main because main( ) is the method called when a Java application begins.
Besides the main method, you can also create additional methods with whatever names you want to use.
(String[] args) :
It’s called a parameter list, and it’s used to pass data to a method.
Java requires that the main method must receive a single parameter that’s an array of String objects.
By convention, this parameter is named args. If you don’t know what a parameter, a String, or an array is, don’t worry about it You can find out what a String, parameters and arrays are in the next chapter.
You have to write (String[] args) on the declaration for the main methods in all your programs. In this case, args receives any command-line arguments present when the program is executed.
This program does not make use of this information, but several other programs use command-line argument.
Another { :
Another set of braces begins at line 4 and ends at line 6. These mark the body of the main method.
Notice that the closing brace in line 6 is paired with the opening brace in line 4, while the closing brace in line 7 is paired with the one in line 2.
This type of pairing is commonplace in Java. In short, whenever you come to a closing brace, it is paired with the most recent opening brace that hasn’t already been closed — that is, that hasn’t already been paired with a closing brace.
System.out.println(“My First JAVA program...............”); :
This is the only statement in the entire program. It calls a method named println that belongs to the System.out object.
System is a predefined class, it is automatically included in your programs that provides access to the system and out is the output stream that is connected to the console.
The println method displays a line of text on the console. The text to be displayed is passed to the println method as a parameter in parentheses following the word println.
In this case, the text is the string literal My First JAVA program............... enclosed in a set of double quotation marks. As a result, this statement displays the text My First JAVA program............... on the console.
Note: In Java, statements end with a semicolon. Because this is the only statement in the program, this line is the only one that requires a semicolon.
Java is case-sensitive. Thus, Main is different from main.
} :
Line 6 contains the closing brace that marks the end of the main method body that was begun by the brace on line 4.
Another } :
Line 7 contains the closing brace that marks the end of the FirstProg class body that was begun by the brace on line 2. Because this program consists of just one class, this line also marks the end of the program.
To run this program, first of all save a text file named FirstProg.java . Then, compile it by running this command at a command prompt:
javac FirstProg.java
This command creates a class file named FirstProg.class that contains
the Java bytecodes compiled for the FirstProg class.
Now run the program by entering this command:
java FirstProg
Output :
My First JAVA program...............
No comments:
Post a Comment