Search

Friday, December 14, 2018

The First Sample Program Line by Line

Although Example  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 begins with the following lines:
/*
This is a simple Java program.
Call this file Example.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. Instead, a comment describes or explains the operation of the program to any one who is reading its source code. In this case, the comment describes the program and reminds you that the source file should be called Example.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 the top of the program is called a multi line 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 multi line comment may be several lines long. The next line of code in the program is shown here:
class Example {
This line uses the key word class to declare that a new class is being defined. As mentioned, the class is Java’s basic unit of encapsulation. Example is the name of the class. The class definition begins with the opening curly brace ({) and ends with the closing curly brace (}). The elements between the two braces are members of the class. For the moment, don’t worry too much about the details of a class except to note that in Java, all program activity occurs within one. This is one reason why all Java programs are (at least a little bit) object-oriented. The next line in the program is the single-line comment,shown here:
// A Java program begins with a call to main().
This is the second type of comment supported by Java. A single-line comment begins with a// and ends at the end of the line. As a general rule, programmers use multi line comments for longer remarks and single-line comments for brief, line-by-line descriptions. The next line of code is shown here:
public static void main (String args[]) {
This line begins the main( )method. As mentioned earlier, in Java, a subroutine is called a method. As the comment preceding it suggests, this is the line at which the program will begin executing. All Java applications begin execution by calling main().The exact meaning of each part of this line can not be given now, since it involves a detailed understanding of several other of Java’s features. However, since many of the examples in this book will use this line of code, let’s take a brief look at each part now. The public keyword is an access specifier.An access specifier determines how other parts of the program can access the members of the class.When a class member is preceded by public, then that member can be accessed by code out side 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 public, since it must be called by code outside of its class when the program is started. The key word static allows main( )to be called before an object of the class has been created. 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 you will see, methods may also return values. If all this seems a bit confusing, don’t worry. All of these concepts will be discussed in detail in subsequent modules. As stated,main( )is the method called when a Java application begins. 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 no parameters are required for a given method, you still need to include the empty parentheses. In main( ) there is only one parameter,String args[ ], which declares a parameter named args. This is an array of objects of type String. (Arrays are collections of similar objects.) Objects of type String store sequences of characters. 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 other programs shown later in this book will. The last character on the line is the {. This signals the start of main( )’s body. All of the code included in a method will occur between the method’s opening curly brace and its closing curly brace. The next line of code is shown here. Notice that it occurs in side main( ).
System.out.println("Java drives the Web.");
This line outputs the string "Java drives the Web." 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. 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. Thus,System.out is an object that encapsulates console output. The fact that Java uses an object to define console output is further evidence of its object-oriented nature. As you have probably guessed, console output (and input) is not used frequently in real-world Java programs and applets. Since most modern computing environments are windowed and graphical in nature, console I/O is used mostly for simple utility programs and for demonstration programs. Later in this book, you will learn other ways to generate output using Java, but for now, we will continue to use the console I/O methods. Notice that the println( )statement ends with a semicolon. All statements in Java end with a semicolon. The reason that the other lines in the program do not end in a semicolon is that they are not, technically, statements. The first } in the program ends main( ), and the last } ends the Example class definition. One last point: Java is case sensitive. Forgetting this can cause you serious problems. For example, if you accidentally type Main instead of main, or Print Ln in stead of println, the preceding program will be incorrect. Furthermore, although the Java compiler will compile classes that do not contain a main( )method, it has no way to execute them. So, if you had miss typed main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( )method.

0 comments:

Post a Comment