Java Reference
In-Depth Information
Sample Run:
The sum = 1226
The sum = 38
38 is the sum
The sum of 12 and 26 = 38
The class String contains many useful methods to manipulate strings. We will take
a closer look at this class in Chapter 3 and illustrate how to manipulate strings. The
complete description of this class can be found at the Web site http://java.sun.com/
javase/7/docs/api/.
Input
As noted earlier, the main objective of Java programs is to perform calculations and
manipulate data. Recall that the data must be loaded into main memory before it can be
manipulated. In this section, you will learn how to put data into the computer's memory.
Storing data in the computer's memory is a two-step process:
1.
Instruct the computer to allocate memory.
2.
Include statements in the program to put the data into the allocated
memory.
Allocating Memory with Named Constants and Variables
When you instruct the computer to allocate memory, you tell it what names to use for each
memory location and what type of data to store in those locations. Knowing the location of
the data is essential because data stored in one memory location might be needed at several
places in the program. As you learned earlier, knowing what data type you have is crucial for
performing accurate calculations. It is also critical to know whether your data must remain
constant throughout program execution or whether it could change.
Some data must not be changed. For example, the pay rate might be the same for all part-
time employees. The value in a conversion formula that converts inches into centimeters
is fixed, because 1 inch is always equal to 2.54 centimeters. When stored in memory, this
type of data must be protected from accidental changes during program execution. In
Java, you can use a named constant to instruct a program to mark those memory
locations in which data is constant throughout program execution.
Named constant: A memory location whose content is not allowed to change during
program execution.
 
 
Search WWH ::




Custom Search