Data and Expression 3
Data and Expression 3
expressions III
Outline
Java Data Types
Variables and Assignment
Expressions
Data Conversion
Interactive Programs
expressions combination of one or more
operators and operands
Arithmetic Expressions
expressions rules
12 / 2 = 6
12.0 / 2.0 = 6.0
10 / 4 = 2
10 / 4.0 = 2.5
4 / 10 = 0
4.0 / 10 = 0.4
12 % 3 = 0
10 % 3 = 1
3 % 10 = 3
op. precedence
operators of the same eg.
precedence are evaluated 3 + 4 * 5
Operator from left to right. (3 + 4 ) * 5
int c = 9 + 6 - 3 * 6 / 2;
use parentheses to
avoid subtle semantic
errors.
Evaluate: 9 + 6 - 3 * 6 / 2
Step 1. 9 + 6 - (3 * 6) / 2
5/3/2.0 // 0.5
Step 2. 9 + 6 - (18 / 2 )
5/(3/2.0) // 3.33
Step 3. (9 + 6) - 9
Step 4. ( 15 - 9 )
Step 5. 6
Quick Check
In what order are the operators evaluated in the following
expressions?
a + b + c + d + e a + b * c - d / e
1 2 3 4 3 1 4 2
a / (b + c) - d % e a / (b * (c + (d - e)))
2 1 4 3 4 3 2 1
expression trees
• The evaluation of a particular expression
can be shown using an expression tree
a /
a + (b – c) / d
- d
b c
assignment revisited
assignment operator has a lower
precedence than the arithmetic operators
count = count + 1;
The increment (++) and decrement (--) operators use only one operand
e.g count++; // count = count + 1;
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
The behavior of some assignment operators depends on the types of the operands
If the operands to the += operator are strings, the assignment operator performs string concatenation
The behavior of an assignment operator (+=) is always consistent with the behavior of the corresponding
operator (+)
the math class
• Math class: contains methods like sqrt and pow
• To compute xn, you write Math.pow(x, n)
• However, to compute x2 it is significantly more efficient simply to compute x * x
• To take the square root of a number, use the Math.sqrt; for example, Math.sqrt(x)
In Java,
Math.pow(x, y) power xy
Math.exp(x) ex
Widening conversions are safe (go from a small data type to a larger one
(such as a short to an int) - Narrowing conversions can lose information
(from a large data type to a smaller one (such as an int to a short)
• assignment conversion
• promotion
• casting
assignment conversion
answer = scan.nextLine();
Reading Input
• The Scanner class is part of the java.util class
library, and must be imported into a program to be
used
• The nextLine method reads all of the input until
the end of the line is found
• See Echo.java
• The details of object creation and class libraries are
discussed further in Chapter 3
//********************************************************************
// Echo.java Author: Lewis/Loftus
//
// Demonstrates the use of the nextLine method of the Scanner class
// to read a string from the user.
//********************************************************************
import java.util.Scanner;
message = scan.nextLine();
import java.util.Scanner;
message = scan.nextLine();
import java.util.Scanner;
public class GasMileage
{
//-----------------------------------------------------------------
// Calculates fuel efficiency on values entered by the user.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int miles;
double gallons, mpg;