CIS 160 Operators and operator precedence
- Use operators
- Explain operator precedence
- Demonstrate how a value can be cast to another data type
Operators and operator precedence
- math: +, -, *, /, %
- assignment: =
- *, /, % are calculated before +, -
- When the precedence is equivalent, calculations for *, /, %, +, - are done from left to right.
- = is called assignment and is evaluated after everything else. The value on the right side is placed
into the variable on the left side. This operator is NOT "equals". There is an operator to check for
equality, but it has two "=" put together and is totally different.
- Parentheses can be used to change the default calculation order.
- The % operator is also called remainder or modulus. It is the integer remainder after division, so 10 % 3
gives a result of 1, 6 % 2 gives a result of 0, and 19 % 4 gives a result of 3.
- Cafe au Lait: Java operators
- Oracle: Java operator precedence
- Oracle: Assignment, arithmetic, and unary operators
- Oracle: Equality, relational, and conditional operators
- Oracle: Bitwise and bit shift operators
- Oracle: Java operator summary
- int a = 5 + 7; // addition, 12 is stored in a
- double b = 3 - 5 * 3; // subtraction and multiplication, -12 is stored in b
- double c = a / b; // division, -1 is stored in c
- double b = (3 - 5) * 3; // subtraction and multiplication, -6 is stored in b
- double c = a / b; // division, -2 is stored in c
- ++ is the increment operator. If x contains the value 5, then x++ increments x to 6
- ++ can be used before or after the variable. The position causes slightly different behavior in expressions - so don't use it with other operators.
- -- is the decrement operator. If x contains the value 5, then x-- decrements x to 4
- -- can be used before or after the variable. The position causes slightly different behavior in expressions - so don't use it with other operators.
- x += y is equivalent to: x = x + y
- x -= y is equivalent to: x = x - y
- x *= y is equivalent to: x = x * y
- x /= y is equivalent to: x = x / y
- There are many other operators. We will cover some additional operators as we need them.
- Place a data type name in parentheses in front of a value or variable (called typecasting) to temporarily make it the data type of the cast.
- 5 / 2 is integer division and produces an integer result of 2 (NOT 2.5)
- (float)5 / 2 is floating-point division and leaves an answer of 2.5
- 5/(float) 2 is floating-point division and leaves an answer of 2.5
- (double)5 / 2 is floating-point division and leaves an answer of 2.5
- 5/(double) 2 is floating-point division and leaves an answer of 2.5
- If a number contains a decimal point, Java assumes it is a "double" (double precision floating point)
- 5.0 / 2 is floating-point division and leaves an answer of 2.5
- 5 / 2.0 is floating-point division and leaves an answer of 2.5
- An "f" trailing a number tells Java the number is a float (single precision floating point)).
- 5f / 2 is floating-point division and leaves an answer of 2.5
- 5 / 2f is floating-point division and leaves an answer of 2.5