Java Reference
In-Depth Information
2.1 Basic Data Concepts
Programs manipulate information, and information comes in many forms. Java is a
type-safe language, which means that it requires you to be explicit about what kind of
information you intend to manipulate and it guarantees that you manipulate the data
in a reasonable manner. Everything that you manipulate in a Java program will be of
a certain type, and you will constantly find yourself telling Java what types of data
you intend to use.
Data Type
A name for a category of data values that are all related, as in type int in
Java, which is used to represent integer values.
A decision was made early in the design of Java to support two different kinds of
data: primitive data and objects. The designers made this decision purely on the basis
of performance, to make Java programs run faster. Unfortunately, it means that you
have to learn two sets of rules about how data works, but this is one of those times
when you simply have to pay the price if you want to use an industrial-strength pro-
gramming language. To make things a little easier, we will study the primitive data
types first, in this chapter; in the next chapter, we will turn our attention to objects.
Primitive Types
There are eight primitive data types in Java: boolean , byte , char , double , float ,
int , long , and short . Four of these are considered fundamental: boolean , char ,
double , and int . The other four types are variations that exist for programs that have
special requirements. The four fundamental types that we will explore are listed in
Table 2.1.
The type names ( int , double , char , and boolean ) are Java keywords that you will
use in your programs to let the compiler know that you intend to use that type of data.
It may seem odd to use one type for integers and another type for real numbers.
Isn't every integer a real number? The answer is yes, but these are fundamentally dif-
ferent types of numbers. The difference is so great that we make this distinction even
in English. We don't ask, “How much sisters do you have?” or “How many do you
weigh?” We realize that sisters come in discrete integer quantities (0 sisters, 1 sister, 2
sisters, 3 sisters, and so on), and we use the word “many” for integer quantities (“How
Table 2.1
Commonly Used Primitive Types in Java
Type
Description
Examples
integers (whole numbers)
int
42, -3, 18, 20493, 0
real numbers
double
7.35, 14.9, -19.83423
single characters
char
'a', 'X', '!'
logical values
boolean
true, false
 
 
Search WWH ::




Custom Search