Let's look at the simple actual java program.
/*
This is a simple Java program.
Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main( ).
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
The first thing that 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
Example.java.
Let’s closely examine each part of the program.
The program begins with the following lines:
/*
This is a simple Java program.
Call this file "Example.java".
*/
This is a
comment. 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 real applications, comments generally explain how some part of the program works or what a specific feature does. Java have three type of comments. This comment is "multiline comment".
The next line of the code in the program,
class Example {
This line uses the keyword
class to declare that a new class is being defined. Example
is an
identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace ({) and the closing curly brace (}).
The next line in the program is the single-line comment,
// Your program begins with a call to main( ).
This is the second type of comment in Java. A single-line comment begins with a
//
and ends at the end of the line.
The next line of code is,
public static void main(String args[]) {
This line begins the main( ) method. All Java applications begin execution by calling main( ).
Let’s take a brief look at each part now.
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 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 the compiler that main( )
does not return a value.
*** Very important think is 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,
String args[ ] declares a parameter named
args, which is an array of instances of the class String. args receives any command-line arguments present when the program is executed.
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.
The next line of code is,
System.out.println("This is a simple Java program.");
This line outputs the string "This is a simple 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 line begins with
System.out. While too complicated to explain in detail at this time, briefly,
System is a predefined class that provides access to the system, and
out is the output stream that is connected to the console.
Finally how can we compile and run this program?
To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line,
C:\>javac Example.java
The javac compiler creates a file called Example.class that contains the bytecode version of the program.the Java bytecode is the intermediate representation of your program that contains instructions the Java Virtual Machine will execute. Thus, the output of javac is not code that can be directly executed.
To actually run the program, you must use the Java application launcher called java. To do so, pass the class name Example as a command-line argument, as shown here:
C:\>java Example
When the program is run, the following output is displayed:
This is a simple Java program.