Java Reference
In-Depth Information
A class that is underlined in red can mean that we have forgotten an import. The
easiest way to fix the error is to right-click on the red code and select Fix Imports .
When you have a program that does not compile, hover with the mouse over the red
parts of the program and see what the errors are. If NetBenas reports that a class
is undefined, you may have misspelled the class or simply forgotten to include an
import. In the second case, selecting Fix Imports will solve the problem. If the line
is still underlined in red after selecting Fix Imports , then the error is not forgetting
to import a library.
Lines 4-16 define the Main class. The opening brace at Line 4 defines the beginning
of the class, while the corresponding closing brace at Line 16 represents the end of the
class. In every class, we can include one or more methods. Inside the Main class, we have
asingle main method: Lines 5-15. Every program must have a main method; this is where
the program starts executing. Note that we have moved the definitions of the variables to
the top of the method. In Java, variables can be declared anywhere throughout the code.
However, it is good software practice to define them at the beginning of a block. Blocks in
Java are defined by opening and closing braces.
Every variable has a scope . This is the context in which the variable is valid. After a
variable goes out of scope, it is no longer valid and it cannot be referenced. The reason
is that the memory for the variable is freed after the variables goes out of scope.
The score of a variable is defined by the inner-most pair of opening/closing braces
surrounding the variable. Of course, the life of a variable starts with its definition and
it cannot be referenced before it is defined.
For example, the scope of the variables x , y ,and c is the main method. This is the
inner-most pair of opening/closing braces surrounding the variable definition. Note that a
variable definition must also precede the first use of the variable. For example, we cannot
write the statement c = 9/5 before the statement double y .
The variables y and c are defined on the same line (Line 7). One can define several
variables in the same statement as long as all the variables are of the same type. Line 8
asks the user to enter the temperature in Celsius. Line 9 declares a Scanner object, that
is, an object that is needed to read input. Line 10 reads an integer from the keyboard and
saves it in the variable x . Lines 11-12 compute the value of c and y , respectively. Note that
the order of the two lines matters. One should not use the variable c before its value is
initialized. Line 13 prints a statement that the temperature in Fahrenheit will be displayed
and Line 14 displays the temperature.
Let us run the program. If we enter 0 degrees Celsius, then we get 32.0 degrees Fahren-
heit. It seems that our program works correctly. Just to be sure, let us run the program
again. If we enter 40 degrees Celsius, we get 72 degrees Fahrenheit. This, however, is incor-
rect output. We can calculate that 32 + 9 ยท
40 = 104. This means that our program has a
bug. Fortunately, we do not have to physically search for the rodent. Instead, we can debug
the program.
Debugging is executing a program line by one. This allows us to examine how the
variables change as the program progresses.
 
Search WWH ::




Custom Search