Java Reference
In-Depth Information
Comments and commands related to packages, such as the import
statement, are the only code allowed outside the class header and its braced
block of code. Recall that during compilation, the import statement loads the
appropriate class or classes from the location where the SDK is stored. Typing
an asterisk (*) after the package name tells the program to load all the classes
within a package; typing an individual class name after the package name tells
the program to load an individual class.
Storing Data
For a computer program to make use of data, the data must be stored in the
memory of the computer. Java makes efficient use of memory by requiring pro-
grammers to declare the kind of data they want to store. Java then allocates just
enough memory for that data. It is a good programming practice to store data in
the smallest location possible while still maintaining the precision and integrity
of the data. Precision refers to the amount of storage allocated to hold the frac-
tional part of a number. If a greater amount of storage is allocated, a more pre-
cise number with additional decimal places can be stored. Thus, the more
precision a system uses, the more exactly it can represent fractional quantities.
Before a Java program can manipulate data, the program must identify the
types of data to be used, declare a variable identifier, and put actual data into the
storage location that the program can access later.
Java Data Types
As you learned in Chapter 2, a data type classifies the data into a particular
category of information and tells the computer how to interpret and store the
data. Humans easily can distinguish between different types of data. For exam-
ple, you usually can tell at a glance whether a number is a percentage, a time, or
an amount of money by the use of special symbols, such as %, :, or $, which
indicate the data type. Similarly, a computer uses special internal codes and
words to keep track of and identify the different types of data it processes.
Java is a strongly typed language , which means it enforces a set of rules
about how you use the objects you create, especially when using different types
of data. For instance, every variable must have a data type, which determines the
values that the variable can contain and the operations a program can perform
on the data. Java programmers thus cannot declare a variable location as an inte-
ger and then try to insert a string of characters into that variable location. There
would not be enough room because each data type has internal sizes associated
with it.
Java supports two categories of data types: primitive and reference. A
primitive data type is a data type that is structured by Java to hold single data
items, such as integer, character, floating point, and true or false values. Java
supports eight primitive data types, which help programmers by restricting the
kind of data allowed in the declared variable location. If you try to store some
other type of value in that variable location, Java displays an error message dur-
ing compilation. The eight primitive data types, their descriptions, and examples
of each are listed in Table 3-2 on the next page.
Search WWH ::




Custom Search