Java Reference
In-Depth Information
Note
Beginning with Java 8, some changes have been introduced. The data
type
int
can also be used to define an unsigned integer from 0 to 2
32
-1. The
data type
long
can be used to define an unsigned integer between 0 and 2
64
-
1. If your application doesn't require negative values, this will offer you a larger
range of valid positive numbers.
You may think that strings (such as
name = "Bart Baesens")
are lacking in Table 2-6. In fact, as
we will discuss later, Java does not have a built-in string type. It offers special facilities to work with
strings. Note that the default value for the
long
data type is
0L
. The L stands for long and is capital-
ized to avoid confusion with the number one (
l
versus
1
). Likewise, the default values for float and
double end with
f
and
d
, respectively.
It is important to define each variable using the appropriate data type. In fact, limiting the range
of a variable can serve as very useful documentation to better understand its meaning during code
inspection and/or maintenance. Furthermore, it can also help save memory if a variable is defined as
byte
instead of
int
(since
byte
is four times smaller than
int
).
literals
A
literal
is a value assigned to a variable of a specific type. An example of this is:
weight = 60;
height = 1.70;
In this example, the equals sign (
=
) is used as an assignment operator to assign the literals
60
and
1.70
to the variables
weight
and
height
, respectively.
Here are some other examples of literals:
boolean overweight = true;
short age = 38;
character initial = 'B';
Note that literals of type
long
,
float
, and
double
can end with the letters
L
/
l
,
F
/
f
, and
D
/
d
,
respectively. Floating point literals can also be expressed in scientific notation using
E
or
e
. This
is illustrated here:
Double bmi = 24.2;
Double bmi = 24.2d;
Float bmi = 0.242e2;
Character literals (
char
) are always enclosed in single quotes and may contain any Unicode charac-
char gbPoundUniSymbol = '\u00A3';
char gbPoundSymbol = '£';
char dollarUniSymbol = '\u0024';
char dollarSymbol = '$';
Search WWH ::
Custom Search