Chapter 3 Operators Flashcards
Operation
Operand
TYPES OF OPERATORS
- Unary
- Binary
- Ternary
Java operators are not necessarily evaluated from left-to-right order
double reward = 3 + 2 * --cookies;
Order of operator precedence
-
Post-unary operators
expression++, expression--
-
Pre-unary operators
++expression, --expression
-
Other unary operators
-, !, ~, +, (type)
-
Multiplication/division/modulus
*, /, %
-
Addition/subtraction
+, -
-
Shift operators
<<, >>, >>>
-
Relational operators
<, >, <=, >=, instanceof
-
Equal to/not equal to
==, !=
-
Logical operators
&, ^, |
-
Short-circuit logical operators
&&, ||
-
Ternary operators
boolean expression ? expression1 :expression2
-
Assignment operators
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=
Unary operators
By definition, a unary operator is one that requires exactly one operand, or variable, to function.
Unary operators
-
Inverts a boolean’s logical value
!
-
Indicates a number is positive, although numbers are assumed to be positive in Java unless accompanied by a negative unary operator
+
-
Indicates a literal number is negative or negates an expression
-
-
Increments a value by 1
++
-
Decrements a value by 1
--
-
Casts a value to a specific type.
(type)
LOGICAL COMPLEMENT
The logical complement operator (!) flips the value of a boolean expression.
NEGATION OPERATORS
negation operator, -, reverses the sign of a numeric expression
int pelican = !5; boolean penguin = -true; boolean peacock = !0;
- will not compile because in Java you cannot perform a logical inversion (!) of a numeric value.
~~~
int pelican = !5; // DOES NOT COMPILE
~~~ - does not compile because you cannot numerically negate a boolean value; you need to use the logical inverse operator.
~~~
boolean penguin = -true; // DOES NOT COMPILE
~~~ - does not compile because you cannot take the logical complement of a numeric value, nor can you assign an integer to a boolean variable.
~~~
boolean peacock = !0; // DOES NOT COMPILE
~~~
in Java, 1 and true are not related in any way, just as 0 and false are not related.
in Java, 1 and true are not related in any way, just as 0 and false are not related.
INCREMENT AND DECREMENT OPERATORS
- ++
- --
pre-increment operator
If the operator is placed before the operand, referred to as the pre-increment operator and the pre-decrement operator, then the operator is applied first and the value returned is the new value of the expression.
post-increment operator
if the operator is placed after the operand, referred to as the post-increment operator and the post-decrement operator, then the original value of the expression is returned, with operator applied after the value is returned.
Binary arithmetic operators
- +, Adds two numeric values
- -, Subtracts two numeric values
- *, Multiplies two numeric values
- /, Divides one numeric value by another
- %, Modulus operator returns the remainder after division of one numeric value by another
ARITHMETIC OPERATORS
Arithmetic operators are often encountered in early mathematics and include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Arithmetic operators also include the unary operators, ++ and –, which we covered already.
All of the arithmetic operators may be applied to any Java primitives, with the exception of boolean.
only the addition operators + and += may be applied to String values, which results in String concatenation.
Adding Parentheses
you can change the order of operation explicitly by wrapping parentheses around the sections you want evaluated first.
Changing the Order of Operation
Verifying Parentheses Syntax
When working with parentheses, you need to make sure they are always valid and balanced.
- does not compile because the parentheses are not
balanced. There is a left-parenthesis with no matching right-parenthesis.
long pigeon = 1 + ((3 * 5) / 3; // DOES NOT COMPILE
- has an equal number of left and right parentheses, but they are not balanced properly. When reading from left to right, a new right-parenthesis must match a previous left-parenthesis. Likewise, all left-parentheses must be closed by right-parentheses before the end of the expression.
int blueJay = (9 + 2) + 3) / (2 * 4; // DOES NOT COMPILE
- does not compile because Java, unlike some
other programming languages, does not allow brackets, [], to be used in place of parentheses.
short robin = 3 + [(4 * 2) + 4]; // DOES NOT COMPILE
modulus operator
The modulus operator, often called the remainder operator, is simply the remainder when two numbers are divided.
- The modulus operation is not limited to positive integer values in Java;
- it may also be applied to negative integers and floating-point numbers.
- For example, if the divisor is 5, then the modulus value of a negative number is between -4 and 0.
- For the exam, though, you are not required to be able to take the modulus of a negative integer or a floating-point number.
- The modulus operation is not limited to positive integer values in Java;
- it may also be applied to negative integers and floating-point numbers.
- For example, if the divisor is 5, then the modulus value of a negative number is between -4 and 0.
- For the exam, though, you are not required to be able to take the modulus of a negative integer or a floating-point number.
Division and Modulus Operators
floor value, it just means the value
without anything after the decimal point. For example, the floor value is 4
for each of the values 4.0, 4.5, and 4.9999999.
floor value, it just means the value without anything after the decimal point.
For example, the floor value is 4 for each of the values 4.0, 4.5, and 4.9999999.
NUMERIC PROMOTION
primitive numeric promotion
Numeric Promotion Rules
Numeric Promotion Rules:
1. If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
2. If one of the values is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value’s data type.
3. Smaller data types, namely, byte, short, and char, are first promoted to int any time they’re used with a Java binary arithmetic operator, even if neither of the operands is int.
4. After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.
applying ++ to a short
value results in a short value.
What is the data type of x * y?
int x = 1; long y = 33; var z = x * y;
If we follow the first rule, since one of the values is long and the other is int and since long is larger than int, then the int value is promoted to a long, and the resulting value is long.
What is the data type of x + y?
double x = 39.21; float y = 2.1; var z = x + y;
This is actually a trick question, as this code will not compile! As you may remember from Chapter 2, floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f. If the value of y was set properly to 2.1f, then the promotion would be similar to the previous example, with both operands being promoted to a double, and the result would be a double value.
What is the data type of x * y?
short x = 10; short y = 3; var z = x * y;
On the last line, we must apply the third rule, namely, that x and y will both be promoted to int before the binary multiplication operation, resulting in an output of type int. If you were to try to assign the value to a short variable without casting, the code would not compile. Pay close attention to the fact that the resulting output is not a short, as we’ll come back to this example in the upcoming “Assigning Values” section.
What is the data type of w * x / y?
short w = 14; float x = 13; double y = 30; var z = w * x / y;
In this case, we must apply all of the rules. First, will automatically be promoted to int solely because it is a short and it is being used in an arithmetic binary operation. The promoted w value will then be automatically promoted to a float so that it can be multiplied with x. The result of w * x will then be automatically promoted to a double so that it can be divided by y, resulting in a double value.
As The Java Language Specification (JLS 3.10.2) states:
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.
Assigning Values
An assignment operator is a binary operator that modifies, or assigns, the variable on the left side of the operator, with the result of the value on the right side of the equation.
The simplest assignment operator is the =assignment
=
Assigns the value on the right to the variable on the left
Java will automatically promote from smaller to larger data types, as you saw in the previous section on arithmetic operators, but it will throw a compiler exception if it detects that you are trying to convert from larger to smaller data types without casting.
Java will automatically promote from smaller to larger data types, as you saw in the previous section on arithmetic operators, but it will throw a compiler exception if it detects that you are trying to convert from larger to smaller data types without casting.