Sams Teach Yourself C in 21 Days

(singke) #1

Data Types ..........................................................................................................


Java data types come in two flavors. The nonobject data types are called the primitive
data types. These will be familiar to you from your experience with C and C++, although
there are some differences in Java. There is another object type for storage and manipula-
tion of strings (text), called String. Each of these types will be covered in turn.

The Primitive Data Types ..............................................................................

Java has eight built-in data types that are called primitive typesbecause they are not
objects. Their names are boolean,char,byte,short,int,long,float, anddouble. The
primitive data types can be separated based on the type of data they can hold.

True/False Data
Use the booleandata type to hold true/false, yes/no data. A type booleanvariable can
hold only the two values trueandfalse. Unlike other programming languages, which
use the numerical value 0 for false and -1for true, the Java booleantype is not based on
numbers.

Integer Numeric Data
Java has four primitive types to hold integer numeric data—that is, numbers with no frac-
tional part. The four types—byte,short,int, andlong—differ in the amount of memo-
ry they take up and in the range of values they can hold. When you need to store an inte-
ger value, select one of these primitive types based on the largest and smallest

708 Bonus Day 4

Java identifiers and keywords are all case-sensitive, just as with C and C++,
so you need to use care when typing. A set of guidelines for identifier
names has been developed, and by following it you reduce the chance of
errors. These guidelines are not mandatory, however, we recommend them.
Class and interface namesare descriptive nouns with the first letter of each
word in uppercase. Examples are BarGraphandAddressList. Interfaces are
sometimes named with the ablesuffix, as in SortableorMailable.
Object and variable names are nouns or noun phrases with the first letter in
lowercase and subsequent words uppercase. Examples are interestRateand
marchReport.
Method names are verbs or verb phrases with the first letter in lowercase
and subsequent words in uppercase. Examples are calculateAverageand
clearAllData.
Constant names are all uppercase with words separated by an underscore.
Examples are MAXIMUM_COUNTandOLD_INTEREST_RATE.

Caution


39 448201x-Bonus4 8/13/02 11:19 AM Page 708

Free download pdf