System, but they may not be reproduced for publication


parts of the program can access the members of the class. When a class member is



Yüklə 83 Mb.
Pdf görüntüsü
səhifə10/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   6   7   8   9   10   11   12   13   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )


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 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 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 before an object of
the class has been created. This is necessary because main( ) is called by the JVM


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 chapters.
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 inside main( ).
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 that 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 applications. Since most modern computing environments are windowed
and graphical in nature, console I/O is used mostly for simple utility programs, for
demonstration programs, and for server­side code. 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. Many statements in Java
end with a semicolon. As you will see, the semicolon is an important part of the Java
syntax.
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 PrintLn instead 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 mistyped 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.
HANDLING SYNTAX ERRORS
If you have not yet done so, enter, compile, and run the preceding program. As you may
know from your previous programming experience, it is quite easy to accidentally type
something incorrectly when entering code into your computer. Fortunately, if you enter
something incorrectly into your program, the compiler will report a syntax error
message when it tries to compile it. The Java compiler attempts to make sense out of
your source code no matter what you have written. For this reason, the error that is
reported may not always reflect the actual cause of the problem. In the preceding
program, for example, an accidental omission of the opening curly brace after the
main( ) method causes the compiler to report the following two errors:
Clearly, the first error message is completely wrong because what is missing is not a
semicolon, but a curly brace.
The point of this discussion is that when your program contains a syntax error, you
shouldn’t necessarily take the compiler’s messages at face value. The messages may be
misleading. You may need to “second­guess” an error message in order to find the real
problem. Also, look at the last few lines of code in your program that precede the line


being flagged. Sometimes an error will not be reported until several lines after the point
at which the error actually occurred.
A SECOND SIMPLE PROGRAM
Perhaps no other construct is as important to a programming language as the
assignment of a value to a variable. A variable is a named memory location that can be
assigned a value. Further, the value of a variable can be changed during the execution of
a program. That is, the content of a variable is changeable, not fixed. The following
program creates two variables called myVar1 and myVar2:
When you run this program, you will see the following output:
This program introduces several new concepts. First, the statement
declares a variable called myVar1 of type integer. In Java, all variables must be
declared before they are used. Further, the type of values that the variable can hold
must also be specified. This is called the type of the variable. In this case, myVar1 can
hold integer values. These are whole number values. In Java, to declare a variable to be
of type integer, precede its name with the keyword int. Thus, the preceding statement


declares a variable called myVar1 of type int.
The next line declares a second variable called myVar2:
Notice that this line uses the same format as the first line except that the name of the
variable is different.
In general, to declare a variable you will use a statement like this:

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   6   7   8   9   10   11   12   13   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə