Outline Character Strings Variables and Assignment Primitive Data
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes
Expressions l Expression l l Combination of one or more operators and operands Arithmetic operations l l Binary operations l Addition(+), Subtraction(-), multiplication(*), Division(/) l Remainder(%) § Example: 17%4 = 1, 3%8 = 8 Unary operations (rarely used) l Example: -1; -4; +5
Result of an arithmetic operation l l If either or both operands l Used by an arithmetic operator are floating point l Then the result is a floating point Result is floating point value l If both or l either operands are floating point values l 3. 4 + 5 = 8. 4
Division l However, division operation is less intuitive l If both operands are integer => integer division l l 10/4 = 2 If either or both are floating point=> floating point division l 10. 0/4 and 10/4. 0 and 10. 0/4. 0 are all 2. 5
Operator precedence l Expressions are evaluated l according to operator precedence hierarchy l It follows the same rules learned in Algebra l l Precedence can be forced by using parentheses l l Multiplications, divisions and remainder are performed § Prior to addition, and subtraction (14+8)/2; Arithmetic operators with the same precedence l Are evaluated from left to right
Operator precedence (cont’d) Highest priority Lowest priority Precedence level Operator Operation 1 + - Unary plus Unary minus 2 * / % Multiplication Division Remainder 3 + - Addition Subtraction 4 = Assignment
Operator Precedence l What is the order of evaluation in the following expressions? a + b + c + d + e 1 2 3 4 a + b * c - d / e 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1
Assignment revisited l The assignment operator l Has a lower precedence than arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = 4 sum / 4 + MAX * lowest; 1 3 Then the result is stored in the variable on the left hand side 2
Assignment revisited (cont’d) l The right and left sides of an assignment l Can contain the same variable First, one is added to the original value of count = count + 1; Then the result is stored back into count (overwriting the original value)
Example l l Program (Temp. Converter) l Converts a particular Celsius temperature value l To its equivalent Fahrenheit value using the expression See Temp. Converter. java
Increment and decrement a variable l There are three ways l To increment or decrement a variable, it may appear l l The left-hand side l l On both the left-hand side and the right-hand side § count = count +1; or count = count – 1; of an increment(++) or decrement(--) operator (postfix form) § count++; or count–-; The right-hand side l of an increment(++) or decrement(--) operator (prefix form) § ++count; or -–count;
Increment and decrement operators l The increment and decrement operators use only l One operand l The increment operator (++) adds one to its operand l l The decrement operator (--) subtracts one from operand l l As such, count++; <=> count = count + 1; As such, count--; <=> count = count - 1; Increment and decrement operators can be used l In postfix form : count++; l or Prefix form: ++count;
Postfix and prefix forms l When used alone l the prefix and postfix forms are equivalent l l It doesn’t matter if you write § count++; or ++count; In a larger expression l they can yield different results l Total = count++; l Total = ++count;
Assignment operators: main idea l l Often we perform l An operation on a variable, and then l Store the result back into that variable Java provides assignment operators l To simplify that process l For instance l num += count; num = num + count;
Assignment operators l Many assignment operators are defined in JAVA l += performs addition l l -= performs subtraction l l result -= a + b; <=> result = result – (a + b); /= performs division l l Total += 5; is equivalent to Total = Total + 5; highest /= 4; <=> highest = highest/4; *= performs multiplication
Behavior of assignment operators l The behavior of some assignment operators l Depends on the types of the operands l If the operands to the += l l Are strings => operator performs string concatenation The behavior of an assignment operator (+=) l is always consistent with the behavior of the l corresponding operator (+)
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes
Data conversion l Sometimes, l It is convenient to convert data from l l For example l in a particular situation we may want to treat l l one type to another An integer as a floating point value These conversions l Do not change the type of a variable l l nor the value stored in it They only convert a value as part of a computation
Data conversion l Widening conversions l It is safe l l Since byte is stored in 8 bits l l l to convert from a byte type to a short type From To byte short, int, long, float, double short int, long, float, double int long, float, or double long float or double float double whereas short in 16 bits There is no loss of information, and the numeric value is preserved exactly
Data conversion (cont’d) l Narrowing conversions l Go from one type l l As such, some of the information l l to a type that uses less space may be compromised In general, they must be avoided From To short byte int byte, short long byte, short, int float byte, short, int, long double byte, short, int, long, float
Conversion techniques l In JAVA, conversion can occur in three ways l l l Assignment conversion l When a value of one type is assigned l to a variable of another type Promotion l When operators need to modify their operands l in order to perform the right operation Casting l The most general form of conversion
Assignment conversion l Assignment conversion occurs l l When a value of one type l is assigned to a variable of another type l if money is a float variable and dollars is an int variable § money = dollars=> dollars converted to float Only widening conversions can be accomplished l Via an assignment l Note that the value or type of dollars did not change
Conversion via promotion l It occurs automatically l When certain operators need l to convert their operands to perform operations l If sum is a float and count is an integer l The value of count is converted to float to perform calculation § result = sum/count;
Conversion using casting l It is l the most general form of conversion in JAVA l a JAVA operator l l Specified by a type name in parentheses: (float) for instance placed in front of the value to be converted l float money = 84. 69; int dollars; dollars = (int) money; => dollars = 84
Casting: analysis l Casting is the most powerful and dangerous l Both widening and narrowing conversions l l Can be accomplished by casting a value To cast l The type is put in parentheses in front of the value l Being converted
Conversion using casting (cont’d) l It is helpful l To treat a value temporarily as another type l For example, if total and count are integers l 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
- Slides: 27