
Chapter 3—Expressions
The Art and Science of
An Introduction
to Computer Science
ERIC S. ROBERTS
Java
Expressions
C H A P T E R 3
“What’s 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
CMPU-101
Computer Science I:
Problem-Solving and Abstraction
Fall 2008
Jennifer Walter
(adapted from slides by Eric Roberts)
Expressions in Java
• The heart of the Add2Integers program from Chapter 2 is
the line
• The n1 + n2 that appears to the right of the equal sign is an
example of an expression, which specifies the operations
involved in the computation.
• An expression in Java consists of terms joined together by
operators.
• Each term must be one of the following:
– A literal constant (such as 3.14159265 or "hello, world")
– A variable name (such as n1, n2, or total)
– A method call that returns a value (such as readInt)
– An expression enclosed in parentheses
int total = n1 + n2;
that performs the actual addition.
Primitive Data Types
• Although complex data values are represented using objects,
Java defines a set of primitive types to represent simple data.
• Of the eight primitive types available in Java, the programs in
our text use only the following four:
int This type is used to represent integers, which are whole
numbers such as 17 or –53.
double This type is used to represent numbers that include a decimal
fraction, such as 3.14159265. In Java, such values are called
floating-point numbers; the nam e double comes from the
fact that the representation uses twice the minimum precision.
char This type represents a single character and is described in
Chapter 8.
boolean This type represents a logical value (true or false).
Summary of the Primitive Types
A data type is defined by a set of values called the domain and a
set of operations. The following table shows the data domains
and common operations for all eight of Java’s primitive types:
Type
short
int
long
float
double
char
boolean
8-bit integers in the range –128 to 127
16-bit integers in the range –32768 to 32767
32-bit integers in the range
–2146483648 to 2146483647
64-bit integers in the range
–9223372036754775808 to 9223372036754775807
32-bit floating-point numbers in the range
± 1.4 x 10-45 to ± 3.4028235 x 10-38
64-bit floating-point numbers in the range
± 4.39 x 10-322 to ± 1.7976931348623157 x 10308
16-bit characters encoded using Unicode
the values true and false
The arithmetic operators:
+
-
*
/
%
add
subtract
remainder
divide
multiply
= =
<
!=
<=
>=
equal to
less than
greater or equal
less or equal
not equal
>greater than
The arithmetic operators except %
The relational operators:
The relational operators
The relational operators
The logical operators:
&& add || or !not
Domain (literals) Common operations
byte
Literals and Variables
• The simplest terms that appear in expressions are literals and
variables. The value of a literal does not change during the
course of a program. A variable is a placeholder for a value
that can be updated as the program runs.
• The format of a constant depends on its type:
– Integral constants consist of a string of digits, optionally preceded by a
minus sign, as in 0, 42, -1, or 1000000.
– Floating-point constants include a decimal point, as in 3.14159265
or 10.0. Floating-point constants can also be expressed in scientific
notation by adding the letter E and an exponent after the digits of the
number, so that 5.646E-8 represents the number 5.646 x 10-8.
– The two constants of type boolean are true and false.
– Character and string constants are discussed in detail in Chapter 8.
For the moment, all you need to know is that a string constant consists
of a sequence of characters enclosed in double quotation marks, such
as "hello, world".
• A variable in Java is most easily envisioned as a box capable
of storing a value.
• Each variable has the following attributes:
–A name, which enables you to differentiate one variable from another.
–A type, which specifies what type of value the variable can contain.
–A value, which represents the current contents of the variable.
total
(contains an int)
42
• The name and type of a variable are fixed. The value changes
whenever you assign a new value to the variable using =
Literals and Variables
• The format of a literal depends on its type:
– Integer literals consist of a st ring of d igits, optionally preceded by a
minus sign, as in 0, 42, -1, or 1000000.
– Floating-point literals include a decimal point, as in 3.14159265 or
10.0. Floating-point literals can also be expressed in scientific
notation by adding the letter E and an exponent after the digits of the
number, so that 5.646E-8 represents the number 5.646 x 10-8.
– The two literals of type boolean are true and false.
– Character and string literals are discussed in detail in Chapter 8. For
the moment, all you need to know is that a string literal consists of a
sequence of characters enclosed in quotation marks, such as "hello,
world” and a char literal is enclosed in single quotes ‘A’.