0% found this document useful (0 votes)
27 views

Ch. 2 - Lecture5 - Arithmetic Expressions and Data Conversion

This document discusses data types and expressions in Java. It covers primitive data types, arithmetic and assignment operators, operator precedence, and data conversion between types. Expressions are evaluated according to operator precedence rules. Assignment operators can be used to simplify operations that assign a result back to a variable. Data conversion, or casting, allows treating data as a different type but may lose precision during narrowing conversions.

Uploaded by

elio saliba
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Ch. 2 - Lecture5 - Arithmetic Expressions and Data Conversion

This document discusses data types and expressions in Java. It covers primitive data types, arithmetic and assignment operators, operator precedence, and data conversion between types. Expressions are evaluated according to operator precedence rules. Assignment operators can be used to simplify operations that assign a result back to a variable. Data conversion, or casting, allows treating data as a different type but may lose precision during narrowing conversions.

Uploaded by

elio saliba
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Outline Character Strings

Variables and Assignment


Primitive Data Types
Expressions
Data Conversion
Interactive Programs
Graphics
Applets
Drawing Shapes
Expressions
 Expression
 Combination of one or more operators and operands

 Arithmetic operations
 Binary operations
 Addition(+), Subtraction(-), multiplication(*), Division(/)

 Remainder(%)
 Example: 17%4 = 1, 3%8 = 8

 Unary operations (rarely used)


 Example: -1; -4; +5
Result of an arithmetic
operation
 If either or both operands
 Used by an arithmetic operator are floating point

 Then the result is a floating point

 Result is floating point value


 If both or

 either operands are floating point values


 3.4 + 5 = 8.4
Division
 However, division operation is less intuitive
 If both operands are integer => integer division
 10/4 = 2

 If either or both are floating point=> floating point division


 10.0/4 and 10/4.0 and 10.0/4.0 are all 2.5
Operator precedence
 Expressions are evaluated
 according to operator precedence hierarchy

 It follows the same rules learned in Algebra


 Multiplications, divisions and remainder are performed
 Prior to addition, and subtraction

 Precedence can be forced by using parentheses


 (14+8)/2;

 Arithmetic operators with the same precedence


 Are evaluated from left to right
Operator precedence (cont’d)
Precedence Operator Operation
Highest level
priority
1 + Unary plus
- Unary minus
2 * Multiplication
/ Division
% Remainder
3 + Addition
- Subtraction
4 = Assignment
Lowest
priority
Operator Precedence
 What is the order of evaluation 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
2 1 4 3

a / (b * (c + (d - e)))
4 3 2 1
Assignment revisited
 The assignment operator
 Has a lower precedence than arithmetic operators

First the expression on the right hand


side of the = operator is evaluated

answer = sum / 4 + MAX * lowest;


4 1 3 2

Then the result is stored in the


variable on the left hand side
Assignment revisited (cont’d)
 The right and left sides of an assignment
 Can contain the same variable

First, one is added to the


original value of count

count = count + 1;

Then the result is stored back into count


(overwriting the original value)
Example
 Program (TempConverter)
 Converts a particular Celsius temperature value

 To its equivalent Fahrenheit value using the expression

9
Fahrenheit  Celsius  32
5
 See TempConverter.java
Increment and decrement a
variable
 There are three ways
 To increment or decrement a variable, it may appear
 On both the left-hand side and the right-hand side
 count = count +1; or count = count – 1;

 The left-hand side


 of an increment(++) or decrement(--) operator (postfix form)
 count++; or count–-;

 The right-hand side


 of an increment(++) or decrement(--) operator (prefix form)
 ++count; or -–count;
Increment and decrement
operators
 The increment and decrement operators use only
 One operand

 The increment operator (++) adds one to its operand


 As such, count++; <=> count = count + 1;

 The decrement operator (--) subtracts one from operand


 As such, count--; <=> count = count - 1;

 Increment and decrement operators can be used


 In postfix form : count++;

 or Prefix form: ++count;


Postfix and prefix forms
 When used alone
 the prefix and postfix forms are equivalent
 It doesn’t matter if you write
 count++; or ++count;

 In a larger expression
 they can yield different results
 Total = count++;

 Total = ++count;
Assignment operators: main
idea
 Often we perform
 An operation on a variable, and then

 Store the result back into that variable

 Java provides assignment operators


 To simplify that process

 For instance
 num += count;  num = num + count;
Assignment operators
 Many assignment operators are defined in JAVA
 += performs addition
 Total += 5; is equivalent to Total = Total + 5;

 -= performs subtraction
 result -= a + b; <=> result = result – (a + b);

 /= performs division
 highest /= 4; <=> highest = highest/4;

 *= performs multiplication
Behavior of assignment
operators
 The behavior of some assignment operators
 Depends on the types of the operands

 If the operands to the +=


 Are strings => operator performs string concatenation

 The behavior of an assignment operator (+=)


 is always consistent with the behavior of the

 corresponding operator (+)


Outline
Character Strings
Variables and Assignment
Primitive Data Types
Expressions
Data Conversion
Interactive Programs
Graphics
Applets
Drawing Shapes
Data conversion
 Sometimes,
 It is convenient to convert data from
 one type to another

 For example
 in a particular situation we may want to treat
 An integer as a floating point value

 These conversions
 Do not change the type of a variable
 nor the value stored in it

 They only convert a value as part of a computation


Data conversion
 Widening conversions
From To
 It is safe
 to convert from a byte type to a short byte short,int,long,
type
float,double
 Since byte is stored in 8 bits short int, long, float,
 whereas short in 16 bits
double
int long, float, or
 There is no loss of information, double
long float or double
 and the numeric value is preserved
exactly float double
Data conversion (cont’d)
 Narrowing conversions From To
 Go from one type
 to a type that uses less space short byte

int byte, short


 As such, some of the information
 may be compromised
long byte, short, int

 In general, they must be avoided float byte, short, int,


long
double byte, short, int,
long, float
Conversion techniques
 In JAVA, conversion can occur in three ways
 Assignment conversion
 When a value of one type is assigned

 to a variable of another type

 Promotion
 When operators need to modify their operands

 in order to perform the right operation

 Casting
 The most general form of conversion
Assignment conversion
 Assignment conversion occurs
 When a value of one type
 is assigned to a variable of another type

 if money is a float variable and dollars is an int variable


 money = dollars=> dollars converted to float

 Only widening conversions can be accomplished


 Via an assignment

 Note that the value or type of dollars did not change


Conversion via promotion
 It occurs automatically
 When certain operators need
 to convert their operands to perform operations

 If sum is a float and count is an integer

 The value of count is converted to float to perform calculation


 result = sum/count;
Conversion using casting
 It is
 the most general form of conversion in JAVA

 a JAVA operator
 Specified by a type name in parentheses: (float) for instance

 placed in front of the value to be converted


 float money = 84.69;
int dollars;
dollars = (int) money; => dollars = 84
Casting: analysis
 Casting is the most powerful and dangerous

 Both widening and narrowing conversions


 Can be accomplished by casting a value

 To cast
 The type is put in parentheses in front of the value

 Being converted
Conversion using casting
(cont’d)
 It is helpful
 To treat a value temporarily as another type

 For example, if total and count are integers


 But we want a floating point result when dividing them
 int total, count;
float result;
result = (float) total / count;
Outline Character Strings
Variables and Assignment
Primitive Data Types
Expressions
Data Conversion
Interactive Programs
Graphics
Applets
Drawing Shapes

You might also like