Summary: Arithmetic Expressions

This lesson is a summary of the main points made in this chapter.

  • Java has five operators for primitive numeric data: +, -, *, /, and %. Each has two operands, but + and - can also have only one operand.
  • Operators within an expression have a hierarchy or precedence. That is, they execute in a certain order, as follows:
     1. Operators within parentheses
     2. The unary operators + and -
     3. The type-cast operator
     4. The operators *, /, and %
     5. The binary operators + and -
  • The data type of a binary operation is real if at least one of its operands is real. The data type is integer if both operands are integers.
  • The result of dividing two int operands is truncated to an integer.
  • We can assign a value whose data type appears in the following list to a variable whose data type is either the same or appears to its right in this list:
    byte → short → int → long → float → double
    In all other cases, a type cast is necessary.
  • A type cast converts a value from one data type to another compatible data type. We precede the value with the desired data type, enclosed in parentheses.
  • The class Math contains methods that perform standard mathematical functions, such as the square root and cosine. We use these methods by writing an expression in this form: Math.method_name(. . .)

Get hands-on with 1200+ tech skills courses.