DOC PREVIEW
FIU COP 2210 - Result-Type of Arithmetic Operations and Mixed-Type Expressions

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

FIU COP 2210 - Result-Type of Arithmetic Operations and Mixed-Type Expressions

Download Result-Type of Arithmetic Operations and Mixed-Type Expressions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Result-Type of Arithmetic Operations and Mixed-Type Expressions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Result-Type of Arithmetic Operations and Mixed-Type Expressions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?