Java Reference

In-Depth Information

Gaining fluency
. You will gain
fluency
in Java only by
doing
. As you read about expressions and

assignment, have your IDE (Interactive Development Environment) running on

your computer and practice typing in expressions and evaluating them. Experi-

ment, experiment! Often! For example, evaluate
5/2
and see what you get.

Type in
Integer.MAX_VALUE
and
Integer.MAXVALUE + 2
and see what they

are. Try calling the mathematical functions with various arguments.

There is a major difference between conventional mathematical expressions

and Java expressions. In mathematics, an expression may have any value. It

might get big, but so what? But when using a programming language like Java,

the values of expressions are stored in a computer, which is a finite, physical

device. There is a tension among the sizes of the values of the expressions, the

space the values occupy, and the speed with which the values can be operated on.

Allowing expressions to have
any
value may cause values to take too much space

and operations to take too long.

Java uses the concept of a
type
to let you declare the range of values that you

want to work with and the operations you want to use. We define
type
:

A
type
is a set of values together with a set of operations on them.

In mathematics, we use the word
integer
for the type consisting of the inte-

ger literals {...,
-3
,
-2
,
-1
,
0
,
1
,
2
, ...} together with these basic integer opera-

tions: negation, addition, subtraction, multiplication, and division.

1.1.1

Type int

In Java, type
int
consists of the integers in the range
-2
31
..2
31
-1
(by which

we mean the set of values {
-2
31
,
-2
31
+ 1
, …,
-1
,
0
,
1
, …
2
31
- 1
}). The

value
2
31
is
2
multiplied by itself
31
times, or
2147483648
.

The usual operations of type
int
are: negation, addition, subtraction, multi-

plication, division, and remainder. (Unary
+
is also available, but it is rarely

used.)
All
int
operations have
int
results.

The smallest and largest values of type
int
are difficult to remember, so

Java gives you a notation for accessing them:

Lesson page

6-2

Activity

1-1.2

Integer.MIN_VALUE
: smallest
int
value:
-2147483648
.

Integer.MAX_VALUE
: largest
int
type:
2147483647
.

To see this, type this expression into Java and see what value it gives you:

Integer.MAX_VALUE

Overflow

If a value of an
int
expression gets outside the range of type
int
,
overflow

occurs. When overflow occurs, the value is changed back into one that is in the

range of type
int
. For example,
2147483647 + 2
in mathematics evaluates to

Search WWH ::

Custom Search