Java Reference

In-Depth Information

value
x
is associated with. If
x
contains
7
, the relation is true; if
x
contains
2
, the

relation is false. Here is a more complex relation concerning variables
x
,
y
, and

z
:
x = y + z
.

The relations given so far were in mathematical notation. Java boolean

expressions are also relations, and relations can also be written in a natural lan-

guage. For example, here are some relations, using
int
variables
x
,
y
,
z
and

String
variable
s
:

Variable
s
contains the character
'g'

The number of characters in
s
is
2*y

The temperature in Ithaca got below
x
on 1 January 1999

x
is the number of values read in so far

The first relation concerns variable s. The second is a relation between variables

s
and
y
. The third is a relation about variable
x
. The fourth could be about a vari-

able
x
in a particular Java program.

Note that the mathematical relation
b=c
is written in Java as
b==c
. When

discussing a program, we rely on mathematical notation; when we have to write

such a relation in Java, we have no recourse but to use the bad notation.

Simplifying relations

Some relations take this form:

Activity

1-6.2

(0) If Bill has black hair,
blackHair
is true.

What is often meant is this:

(1) If Bill has black hair,
blackHair
is true; else,
blackHair
is false.

But these two relations mean different things. The first does not say what value

blackHair
has if Bill has red hair, while the second says that if Bill has red hair,

blackHair
is false. Some might say that it is implicit in (0) that
blackHair
is

false if Bill has red hair, but mathematical convention disagrees.

There is a much simpler alternative for the second relation. It is shorter and

doesn't have any case analysis:

(2)
blackHair =
“Bill has black hair”

We have placed quotes around the sub-relation to make clear that it is a unit. The

quoted phrase is itself a relation; the equality says that
blackHair
is equal to the

value of that relation.

Let us show why relations (1) and (2) mean the same thing. In the case that

Bill does have black hair, (1) reduces to “
blackHair
is true”, while (2) reduces

to “
blackHair
= true”. These two are equivalent, so in the case that Bill has

black hair, (1) and (2) mean the same thing.

In the case that Bill does not have black hair, relation (1) reduces to

“
blackHair
is
false
”, while relation (2) reduces to “
blackHair =
false
”. Again,

these two are equivalent, so in the case that Bill does not have black hair, (1) and

Search WWH ::

Custom Search