Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.

Similar presentations


Presentation on theme: "Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes."— Presentation transcript:

1 Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

2 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

3 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

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

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

6 Operator precedence (cont’d) Precedence level OperatorOperation 1+-+- Unary plus Unary minus 2*/%*/% Multiplication Division Remainder 3+-+- Addition Subtraction 4=Assignment Highest priority Lowest priority

7 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123

8 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 Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1432

9 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 Then the result is stored back into count (overwriting the original value) count = count + 1;

10 Example Program (TempConverter) Converts a particular Celsius temperature value To its equivalent Fahrenheit value using the expression See TempConverter.java

11 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;

12 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;

13 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;

14 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;

15 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

16 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 (+)

17 Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

18 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

19 Data conversion Widening conversions It is safe to convert from a byte type to a short type Since byte is stored in 8 bits whereas short in 16 bits There is no loss of information, and the numeric value is preserved exactly FromTo byteshort,int,long, float,double shortint, long, float, double intlong, float, or double longfloat or double floatdouble

20 Data conversion (cont’d) Narrowing conversions Go from one type to a type that uses less space As such, some of the information may be compromised In general, they must be avoided FromTo shortbyte intbyte, short longbyte, short, int floatbyte, short, int, long doublebyte, short, int, long, float

21 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

22 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

23 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;

24 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

25 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

26 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;

27 Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes


Download ppt "Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes."

Similar presentations


Ads by Google