Professional Documents
Culture Documents
expressions in Java
Variables
A variable is a named memory location
capable of storing data
As we have already seen, object variables
refer to objects, which are created by
instantiating classes with the new operator
We can also store data in simple variables,
which represent data only, without any
associated methods
Minimum value
Maximum value
byte
-128
127
short
-32,768
32,767
int
-2,147,483,648
2,147,483,647
long
9,223,372,036,85
4,775,808
9,223,372,036,85
4,775,807
Minimum value
Maximum value
-3.40282347 x 1038
3.40282347 x 1038
double
1.79769313486231
570 x 10308
1.79769313486231
570 x 10308
Assignment statements
We can store a value in a variable using an
assignment statement
Assignment statement syntax:
variableName = expression;
variableName must be the name of a declared
variable
expression must evaluate to an appropriate
value for storage within the type of variable
specified
Arithmetic expressions
An expression is a set of symbols that represents a
value
An arithmetic expression represents a numeric value
Simple expressions are single values; examples:
18
-4
1.245e3
Operation
Symbol
Addition
Subtraction
Multiplication
Division
Modulus
Examples
int x = 4, y = 9, z;
z = x + y * 2;
z = (x + y) * 2;
y = y 1;
// result is 22
// result is 26
// result is 8
Integer division
When one real number is divided by another, the
result is a real number; for example:
double x = 5.2, y = 2.0, z;
z = x / y;
// result is 2.6
Integer division
There are two ways to divide integers
using the / operator, produces the quotient of the two
operands
using the % operator, produces the remainder when
the operands are divided. This is called modular
division, or modulus (often abbreviated mod). For
example:
int
z =
z =
z =
x
x
y
x
=
%
%
%
4, y = 9, z;
2; // result is 0
x; // result is 1
y; // result is 4
Mixed-type expressions
A mixed-type expression is one that involves operands of
different data types
Like other expressions, such an expression will evaluate to a single
result
The data type of that value will be the type of the operand with the
highest precision
What this means, for all practical purposes, is that, if an
expression that involves both real numbers and whole
numbers, the result will be a real number.
// z = 2.5
// z = 2.0
Assignment conversion
Another kind of implicit conversion can
take place when an expression of one type
is assigned to a variable of another type
For example, an integer can be assigned to a
real-number type variable; in this case, an
implicit promotion of the integer value
occurs
No demotions in assignment
conversions
In Java we are not allowed to demote a higherprecision type value by assigning it to a lowerprecision type variable
Instead, we must do an explicit type cast. Some
examples:
int x = 10;
double y = x;
x = y;
y = y / 3;
x = (int)y;
Compound arithmetic/assignment
operators
Previous examples in the notes have included the following
statements:
y = y + 1;
y = y / 3;
Compound arithmetic/assignment
operators
Operator
Use
Meaning
+=
X += 1;
X = X + 1;
-=
X -= 1;
X = X 1;
*=
X *= 5;
X = X * 5;
/=
X /= 2;
X = X / 2;
%=
X %= 10;
X = X % 10;
Named constants
A variable is a named memory location that can hold a
value of a specific data type; as we have seen, the value
stored at this location can change throughout the execution
of a program
If we want to maintain a value in a named location, we use
the Java keyword final in the declaration and immediately
assign the desired value; with this mechanism, we declare
a named constant. Some examples:
final int LUCKY = 7;
final double PI = 3.14159;
final double LIGHTSPEED = 3.0e10.0 ;
Named constants
The name of the constant is used in expressions but cannot
be assigned a new value. For example, to calculate the
value of variable circleArea using the variable radius and
the value , we could write:
circleArea = 2 * PI * radius * radius;
Example continued
If, for example, the discount changes to 12%, the
programmer who has to maintain the code would have to
change the value .1 to .12 everywhere in the program
at least, everywhere that it actually refers to the discount.
The value .1 could very well mean something else in a
different expression.
If we use named constants instead, the value has to change in
just one place, and there is no ambiguity about what the
number means in context; with named constants, the revised
code might read:
total = total (total * discount) + ((total * discount) * (1 + taxrate));
Example
// computing the roots of a quadratic equation:
double
a,
// coefficient of x squared
b,
// coefficient of x
c,
// 3rd term in equation
x1,
// first root
x2;
// second root
// read in values for a, b, and c not shown here
x1 = (-b + Math.sqrt(Math.pow(b, 2) (4 * a * c))) / (2 * a);
x2 = (-b - Math.sqrt(Math.pow(b, 2) (4 * a * c))) / (2 * a);