System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə14/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   10   11   12   13   14   15   16   17   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

var = expression;
Here, the type of var must be compatible with the type of expression.
The assignment operator does have one interesting attribute that you may not be
familiar with: it allows you to create a chain of assignments. For example, consider this


fragment:
This fragment sets the variables x, y, and z to 100 using a single statement. This works
because the = is an operator that yields the value of the right­hand expression. Thus,
the value of z = 100 is 100, which is then assigned to y, which in turn is assigned to x.
Using a “chain of assignment” is an easy way to set a group of variables to a common
value.
SHORTHAND ASSIGNMENTS
Java provides special shorthand assignment operators that simplify the coding of
certain assignment statements. Let’s begin with an example. The assignment statement
shown here
can be written, using Java shorthand, as
Ask the Expert
Q
: Since the short­circuit operators are, in some cases, more efficient
than their normal counterparts, why does Java still offer the normal
AND and OR operators?
A
: In some cases you will want both operands of an AND or OR operation to be
evaluated because of the side effects produced. Consider the following:


As the comments indicate, in the first if statement, i is incremented whether the if
succeeds or not. However, when the short­circuit operator is used, the variable i is
not incremented when the first operand is false. The lesson here is that if your code
expects the right­hand operand of an AND or OR operation to be evaluated, you
must use Java’s non­short­circuit forms of these operations.
The operator pair += tells the compiler to assign to x the value of x plus 10. Here is
another example. The statement
is the same as
Both statements assign to x the value of x minus 100.
This shorthand will work for all the binary operators in Java (that is, those that require
two operands). The general form of the shorthand is
var op = expression;
Thus, the arithmetic and logical shorthand assignment operators are the following:


Because these operators combine an operation with an assignment, they are formally
referred to as compound assignment operators.
The compound assignment operators provide two benefits. First, they are more
compact than their “longhand” equivalents. Second, in some cases, they are more
efficient. For these reasons, you will often see the compound assignment operators used
in professionally written Java programs.
TYPE CONVERSION IN ASSIGNMENTS
In programming, it is common to assign one type of variable to another. For example,
you might want to assign an int value to a float variable, as shown here:
When compatible types are mixed in an assignment, the value of the right side is
automatically converted to the type of the left side. Thus, in the preceding fragment, the
value in i is converted into a float and then assigned to f. However, because of Java’s
strict type checking, not all types are compatible, and thus, not all type conversions are
implicitly allowed. For example, boolean and int are not compatible.
When one type of data is assigned to another type of variable, an automatic type
conversion will take place if

The two types are compatible.

The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place. For example,
the int type is always large enough to hold all valid byte values, and both int and byte
are integer types, so an automatic conversion from byte to int can be applied.
For widening conversions, the numeric types, including integer and floating­point
types, are compatible with each other. For example, the following program is perfectly


valid since long to double is a widening conversion that is automatically performed.
Although there is an automatic conversion from long to double, there is no automatic
conversion from double to long, since this is not a widening conversion. Thus, the
following version of the preceding program is invalid.
There are no automatic conversions from the numeric types to char or boolean. Also,
char and boolean are not compatible with each other. However, an integer literal can
be assigned to char.
CASTING INCOMPATIBLE TYPES
Although the automatic type conversions are helpful, they will not fulfill all
programming needs because they apply only to widening conversions between
compatible types. For all other cases you must employ a cast. A cast is an instruction to
the compiler to convert one type into another. Thus, it requests an explicit type


conversion. A cast has this general form:
(target­type) expression
Here, target­type specifies the desired type to convert the specified expression to. For
example, if you want to convert the type of the expression x/y to int, you can write
Here, even though x and y are of type double, the cast converts the outcome of the
expression to int. The parentheses surrounding x / y are necessary. Otherwise, the cast
to int would apply only to the x and not to the outcome of the division. The cast is
necessary here because there is no automatic conversion from double to int.
When a cast involves a narrowing conversion, information might be lost. For example,
when casting a long into a short, information will be lost if the long’s value is greater
than the range of a short because its high­order bits are removed. When a floating­
point value is cast to an integer type, the fractional component will also be lost due to
truncation. For example, if the value 1.23 is assigned to an integer, the resulting value
will simply be 1. The 0.23 is lost.
The following program demonstrates some type conversions that require casts:


The output from the program is shown here:
In the program, the cast of (x / y) to int results in the truncation of the fractional
component, and information is lost. Next, no loss of information occurs when b is
assigned the value 100 because a byte can hold the value 100. However, when the
attempt is made to assign b the value 257, information loss occurs because 257 exceeds
a byte’s maximum value. Finally, no information is lost, but a cast is needed when
assigning a byte value to a char.
OPERATOR PRECEDENCE
Table 2­3
 shows the order of precedence for all Java operators, from highest to lowest.
This table includes several operators that will be discussed later in this book. Although
technically separators, the [], (), and . can also act like operators. In that capacity, they
would have the highest precedence.


Table 2­3 The Precedence of the Java Operators
Try This 2­2
Display a Truth Table for the Logical Operators
In this project, you will create a program that displays the truth table for Java’s logical
operators. You must make the columns in the table line up. This project makes use of
several features covered in this chapter, including one of Java’s escape sequences and
the logical operators. It also illustrates the differences in the precedence between the
arithmetic + operator and the logical operators.
1. Create a new file called LogicalOpTable.java.
2. To ensure that the columns line up, you will use the \t escape sequence to embed
tabs into each output string. For example, this println( ) statement displays the
header for the table:
3. Each subsequent line in the table will use tabs to position the outcome of each
operation under its proper heading.


4. Here is the entire LogicalOpTable.java program listing. Enter it at this time.
Notice the parentheses surrounding the logical operations inside the println( )
statements. They are necessary because of the precedence of Java’s operators. The +
operator is higher than the logical operators.
5. Compile and run the program. The following table is displayed.
6. On your own, try modifying the program so that it uses and displays 1’s and 0’s,
rather than true and false. This may involve a bit more effort than you might at first


think!
EXPRESSIONS
Operators, variables, and literals are constituents of expressions. You probably already
know the general form of an expression from your other programming experience, or
from algebra. However, a few aspects of expressions will be discussed now.
Type Conversion in Expressions
Within an expression, it is possible to mix two or more different types of data as long as
they are compatible with each other. For example, you can mix short and long within
an expression because they are both numeric types. When different types of data are
mixed within an expression, they are all converted to the same type. This is
accomplished through the use of Java’s type promotion rules.
First, all char, byte, and short values are promoted to int. Then, if one operand is a
long, the whole expression is promoted to long. If one operand is a float operand, the
entire expression is promoted to float. If any of the operands is double, the result is
double.
It is important to understand that type promotions apply only to the values operated
upon when an expression is evaluated. For example, if the value of a byte variable is
promoted to int inside an expression, outside the expression, the variable is still a
byte. Type promotion only affects the evaluation of an expression.
Type promotion can, however, lead to somewhat unexpected results. For example,
when an arithmetic operation involves two byte values, the following sequence occurs:
First, the byte operands are promoted to int. Then the operation takes place, yielding
an int result. Thus, the outcome of an operation involving two byte values will be an
int. This is not what you might intuitively expect. Consider the following program:


Somewhat counterintuitively, no cast is needed when assigning b*b to i, because b is
promoted to int when the expression is evaluated. However, when you try to assign b *
b to b, you do need a cast—back to byte! Keep this in mind if you get unexpected type­
incompatibility error messages on expressions that would otherwise seem perfectly OK.
This same sort of situation also occurs when performing operations on chars. For
example, in the following fragment, the cast back to char is needed because of the
promotion of ch1 and ch2 to int within the expression:
Without the cast, the result of adding ch1 to ch2 would be int, which can’t be assigned
to a char.
Casts are not only useful when converting between types in an assignment. For
example, consider the following program. It uses a cast to double to obtain a fractional
component from an otherwise integer division.


The output from the program is shown here:
Spacing and Parentheses
An expression in Java may have tabs and spaces in it to make it more readable. For
example, the following two expressions are the same, but the second is easier to read:
Parentheses increase the precedence of the operations contained within them, just like
in algebra. Use of redundant or additional parentheses will not cause errors or slow
down the execution of the expression. You are encouraged to use parentheses to make
clear the exact order of evaluation, both for yourself and for others who may have to


figure out your program later. For example, which of the following two expressions is
easier to read?

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   10   11   12   13   14   15   16   17   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə