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.