1079 pág.

# Thinking in Java - 4th Edition

DisciplinaProgramação Orientada A Objetos4.909 materiais74.414 seguidores
Pré-visualização50 páginas
```x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
//! boolean bl = (boolean)x;
char c = (char)x;
88 Thinking in Java Bruce Eckel
byte b = (byte)x;
short s = (short)x;
int i = (int)x;
float f = (float)x;
double d = (double)x;
}
void floatTest(float x, float y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
// Casting:
//! boolean bl = (boolean)x;
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
double d = (double)x;
}
void doubleTest(double x, double y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
Operators 89
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
// Casting:
//! boolean bl = (boolean)x;
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
}
} ///:~
Note that boolean is quite limited. You can assign to it the values true and false, and you
can test it for truth or falsehood, but you cannot add booleans or perform any other type of
operation on them.
In char, byte, and short, you can see the effect of promotion with the arithmetic operators.
Each arithmetic operation on any of those types produces an int result, which must be
explicitly cast back to the original type (a narrowing conversion that might lose information)
to assign back to that type. With int values, however, you do not need to cast, because
everything is already an int. Don\u2019t be lulled into thinking everything is safe, though. If you
multiply two ints that are big enough, you\u2019ll overflow the result. The following example
demonstrates this:
//: operators/Overflow.java
// Surprise! Java lets you overflow.

public class Overflow {
public static void main(String[] args) {
90 Thinking in Java Bruce Eckel
Operators 91
int big = Integer.MAX_VALUE;
System.out.println(&quot;big = &quot; + big);
int bigger = big * 4;
System.out.println(&quot;bigger = &quot; + bigger);
}
} /* Output:
big = 2147483647
bigger = -4
*///:~
You get no errors or warnings from the compiler, and no exceptions at run time. Java is good,
but it\u2019s not that good.
Compound assignments do not require casts for char, byte, or short, even though they are
performing promotions that have the same results as the direct arithmetic operations. On the
other hand, the lack of the cast certainly simplifies the code.
You can see that, with the exception of boolean, any primitive type can be cast to any other
primitive type. Again, you must be aware of the effect of a narrowing conversion when
casting to a smaller type; otherwise, you might unknowingly lose information during the cast.
Exercise 14: (3) Write a method that takes two String arguments and uses all the
boolean comparisons to compare the two Strings and print the results. For the == and !=,
also perform the equals( ) test. In main( ), call your method with some different String
objects.
Summary
If you\u2019ve had experience with any languages that use C-like syntax, you can see that the
operators in Java are so similar that there is virtually no learning curve. If you found this
chapter challenging, make sure you view the multimedia presentation Thinking in C,
available at www.MindView.net.
Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide,
available for sale from www.MindView.net.
Controlling Execution
Like a sentient creature, a program must manipulate its world and
make choices during execution. In Java you make choices with
execution control statements.
Java uses all of C\u2019s execution control statements, so if you\u2019ve programmed with C or C++,
then most of what you see will be familiar. Most procedural programming languages have
some kind of control statements, and there is often overlap among languages. In Java, the
keywords include if-else, while, do-while, for, return, break, and a selection statement
called switch. Java does not, however, support the much-maligned goto (which can still be
the most expedient way to solve certain types of problems). You can still do a goto-like jump,
but it is much more constrained than a typical goto.
true and false
All conditional statements use the truth or falsehood of a conditional expression to determine
the execution path. An example of a conditional expression is a == b. This uses the
conditional operator == to see if the value of a is equivalent to the value of b. The expression
returns true or false. Any of the relational operators you\u2019ve seen in the previous chapter can
be used to produce a conditional statement. Note that Java doesn\u2019t allow you to use a number
as a boolean, even though it\u2019s allowed in C and C++ (where truth is nonzero and falsehood
is zero). If you want to use a non-boolean in a boolean test, such as if(a), you must first
convert it to a boolean value by using a conditional expression, such as if(a != 0).
if-else
The if-else statement is the most basic way to control program flow. The else is optional, so
you can use if in two forms:
if(Boolean-expression)
statement
or
if(Boolean-expression)
statement
else
statement
The Boolean-expression must produce a boolean result. The statement is either a simple
statement terminated by a semicolon, or a compound statement, which is a group of simple
statements enclosed in braces. Whenever the word \u201cstatement\u201d is used, it always implies that
the statement can be simple or compound.
As an example of if-else, here is a test( ) method that will tell you whether a guess is above,
below, or equivalent to a target number:
//: control/IfElse.java
import static net.mindview.util.Print.*;

public class IfElse {

static int result = 0;
static void test(int testval, int target) {
if(testval > target)
result = +1;
else if(testval < target)
result = -1;
else
result = 0; // Match
}
public static void main(String[] args) {
test(10, 5);
print(result);
test(5, 10);
print(result);
test(5,```