Result-Type of Arithmetic Operationsand Mixed-Type ExpressionsComputer Programming I Instructor: Greg ShawCOP 2210 Result-Type of Arithmetic Operationsand Mixed-Type Expressions- Why This Is ImportantIn the assignment statementvariable = expression ;the expression must evaluate to a type that is compatible withthe type of the variable in which it will be stored. The data type of the result of an arithmetic operation is knownas the result-type- The Rules: How Java Determines the Result-Type of an Operation1. If both operands are the same type, then the result is thattype. (E.g., if both operands are double the result is double; ifboth operands are int, the result is an int.)2. If the operands are of different types (i.e., a “mixed-type”expression), then Java will “promote” the result to the“higher” (or “larger”) type.- The Hierarchy of the Types (based on the range of values that canbe stored in each)double — float — long — int — short — bytehighest <----------------------------------> lowestSo, for example, the result of an operation involving a float anda short would be a float, and a long and an int would yield along result.- Summarizing the Rules Since there is generally no good reason to use any types otherthan double and int, we can summarize the two rules like this: If both operands are int, the result is int; otherwise, theresult is double.Always remember that the division of one int by another yields anint result – the integer portion of the quotient (i.e., thequotient “truncated” to an
View Full Document