Expressions in Java
Expressions in Java
CHAPTER 3
ERIC S. ROBERTS
Expressions
Whats twice eleven? I said to Pooh.
(Twice what? said Pooh to Me.)
I think that it ought to be twenty-two.
Just what I think myself, said Pooh.
A. A. Milne, Now We Are Six, 1927
3.1
3.2
3.3
3.4
3.5
3.6
An Introduction
to Computer Science
Expressions in Java
The heart of the Add2Integers program from Chapter 2 is
the line
int total = n1 + n2;
double
Type
Common operations
double
char
boolean
byte
short
int
long
float
! not
total
minus sign, as in 0, 42, -1
, or 1000000.
Floating-point constants include42a decimal point,
as in 3.14159265
(contains
an int) or
10.0. Floating-point constants can also be expressed in scientific
notation
by adding
thefollowing
letter E andattributes:
an exponent after the digits of the
Each
variable
has the
number, so that 5.646E-8 represents the number 5.646 x 10-8.
A name, which enables you to differentiate one variable from another.
The
two constants of type boolean are true and false.
A type, which
specifies
what type
of value the
variable
can contain.
Character
and string
constants
are discussed
in detail
in Chapter
8. For
the
A value,
which
current
contents
of constant
the variable.
moment,
all represents
you need tothe
know
is that
a string
consists of a
of characters enclosed in double quotation marks, such as
Thesequence
name
and
type of a variable are fixed. The value changes
"hello, world".
Java Identifiers
Names for variables (and other things) are called identifiers.
Identifiers in Java conform to the following rules:
A variable name must begin with a letter or the underscore character.
The remaining characters must be letters, digits, or underscores.
The name must not be one of Javas reserved words:
abstract
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while
Variable Declarations
In Java, you must declare a variable before you can use it. The
declaration establishes the name and type of the variable and, in
most cases, specifies the initial value as well.
The most common form of a variable declaration is
The most common operators in Java are the ones that specify
arithmetic computation:
+ Addition
* Multiplication
Subtraction
/ Division
% Remainder
Operators in Java usually appear between two
subexpressions, which are called its operands. Operators
that take two operands are called binary operators.
The - operator can also appear as a unary operator, as in the
expression -x, which denotes the negative of x.
c 132
+ 32
100
1
32
c +
32
4
0
7
Precedence
If an expression contains more than one operator, Java uses
precedence rules to determine the order of evaluation. The
arithmetic operators have the following relative precedence:
unary -
(type cast)
/
highest
%
-
lowest
32
32
0
3
4
30
( 1 + 2 ) % 3 * 4 + 5 * 6 / 7 * ( 8 % 9 ) + 10
Assignment Statements
You can change the value of a variable in your program by
using an assignment statement, which has the general form:
variable = expression;
Shorthand Assignments
Statements such as
total = total + value;
x += 1;
Boolean Expressions
In many ways, the most important primitive
type in Java is boolean, even though it is by
far the simplest. The only values in the
boolean domain are true and false, but
these are exactly the values you need if you
want your program to make decisions.
The name boolean comes from the English
mathematician George Boole who in 1854
wrote a book entitled An Investigation into
the Laws of Thought, on Which are Founded
the Mathematical Theories of Logic and
Probabilities. That book introduced a system
of logic that has come to be known as
Boolean algebra, which is the foundation for
the boolean data type.
George Boole (1791-1871)
Boolean Operators
The operators used with the boolean data type fall into two
categories: relational operators and logical operators.
There are six relational operators that compare values of other
types and produce a boolean result:
= = Equals
!= Not equals
< Less than
<= Less than or equal to
>= Greater than or equal to
> Greater than
For example, the expression n <= 10 has the value true if x
is less than or equal to 10 and the value false otherwise.
There are also three logical operators:
p && q means both p and q
&& LogicalAND
|| Logical OR
p || q means either p or q (orboth)
!p means the opposite of p
! Logical NOT
Short-Circuit Evaluation
Java evaluates the && and || operators using a strategy called
short-circuit mode in which it evaluates the right operand only
if it needs to do so.
TheEnd