Java Reference
In-Depth Information
Java has basic types for characters, different kinds of integers, and different kinds of
floating-point numbers (numbers with a decimal point), as well as a type for the values
true
and
false
. These basic types are known as
primitive types
. Display 1.2 shows
all of Java's primitive types. The preferred type for integers is
int
. The type
char
is
the type for single characters and can store common Unicode characters. The preferred
type for floating-point numbers is
double
. The type
boolean
has the two values
true
and
false
. (Unlike some other programming languages, the Java values
true
and
false
are not integers and will not be automatically converted to integers.) Objects of
the predefined class
String
represent strings of characters.
String
is not a primitive
type, but is often considered a basic type along with the primitive types. The class
String
is discussed later in this chapter.
primitive
types
Assignment Statements
The most direct way to change the value of a variable is to use an
assignment
statement
. In Java, the equal sign is used as the
assignment operator
. An assignment
statement always consists of a variable on the left-hand side of the assignment operator
(the equal sign) and an expression on the right-hand side. An assignment statement
ends with a semicolon. The expression on the right-hand side of the equal sign may be
a variable, a number, or a more complicated expression made up of variables, numbers,
assignment
statement
assignment
operator
Display 1.2
Primitive Types
MEMORY
USED
TYPE NAME KIND OF VALUE
SIZE RANGE
boolean
true
or
false
1 byte
Not applicable
char
Single character
(Unicode)
2 bytes
Common Unicode characters
byte
Integer
1 byte
-
128 to 127
short
Integer
2 bytes
-
32768 to 32767
int
Integer
4 bytes
-
2147483648 to 2147483647
long
Integer
8 bytes
-
9223372036854775808 to
9223372036854775807
float
10
+38
to
Floating-point
number
4 bytes
;
3.40282347
*
10
-45
;
1.40239846
*
double
;1.76769313486231570 * 10
+308
to
;4.94065645841246544 * 10
-324
Floating-point
number
8 bytes