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. The preferred type for floating-point numbers is double .
The type boolean has the two values true and false . (Unlike some other program-
ming languages, the Java values true and false are not integers and will not be auto-
matically 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
Display 1.2 Primitive Types
TYPE NAME
KIND OF VALUE
MEMORY USED
SIZE RANGE
boolean
true or false
1 byte
not applicable
char
single character
(Unicode)
2 bytes
all 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
× 10 + 38 to
float
floating-point
number
4 bytes
±3.40282347
× 10 - 45
±1.40239846
± ±1.76769313486231570 × 10 + 308
to
± ±4.94065645841246544 × 10 - 324
double
floating-point
number
8 bytes
Assignment Statements
The most direct way to change the value of a variable is to use an assignment state-
ment . 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 oper-
ator (the equal sign) and an expression on the right-hand side. An assignment state-
ment 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
assignment
statement
assignment
operator
 
Search WWH ::




Custom Search