Java Reference
In-Depth Information
Java data types
Java is a strongly typed language. This means that every variable should first be carefully declared
upfront before it can be used. You already saw examples of this in the BMIConstructor class, as
follows:
// declare variables
double weight;
double height;
double BMI;
The data type of a variable specifies the kind of values it can be assigned. For example, the weight ,
height , and BMI variables are declared as double variables, so they can only be assigned floating-
point numbers from a specific range. A data type tells the compiler how much memory to allocate
to a variable, the format in which it will be stored, and the operations that can run on it. Although
a variable can change value during program execution, its type always remains fixed. A distinction
can be made between primitive and composite data types. A primitive data type is a basic building
block supported by Java. On the other hand, a composite data type is composed of primitive data
types using a composition construct. The following sections elaborate on both of these data types.
primitive data types
Java supports eight built-in primitive data types. Table 2-6 defines each of these and specifies the
range and default value. If the user has not initialized a variable, the compiler will automatically
assign the default value. Note that the ranges and default values are uniform and do not depend on
the underlying machine architecture on which the Java program runs.
tableĀ 2-6: Java Primitive Data Types
t ype
definition
minimum
ma ximum
default
8-bit signed integer
-128
127
0
byte
18-bit signed integer
32,768
32,767
0
short
32-bit signed integer
-2 31
2 31
0
int
-2 63
2 63 -1
64-bit signed integer
0L
long
1.40239846x10 -45
3.40282347x10 38
Single-precision 32-bit IEEE
754 floating point number
0.0f
float
Double-precision 64-bit IEEE
754 floating point number
4.9406564581246544
x10 -324
1.79769313486231570
x10 -308
0.0d
double
boolean One bit of information; flag
indicator
false
true
false
Single 16-bit Unicode character
'\u0000' (or 0)
'\uffff' (or 65,535)
'\u0000'
char
 
Search WWH ::




Custom Search