Operation
Operand
TYPES OF OPERATORS
Java operators are not necessarily evaluated from left-to-right order
double reward = 3 + 2 * --cookies;
Order of operator precedence
expression++, expression--++expression, --expression-, !, ~, +, (type)*, /, %+, -<<, >>, >>><, >, <=, >=, instanceof==, !=&, ^, |&&, ||boolean expression ? expression1 :expression2=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=
Unary operators
By definition, a unary operator is one that requires exactly one operand, or variable, to function.
Unary operators
!
+
-
++
--
(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;
int pelican = !5; // DOES NOT COMPILE
boolean penguin = -true; // DOES NOT COMPILE
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
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.
long pigeon = 1 + ((3 * 5) / 3; // DOES NOT COMPILE
int blueJay = (9 + 2) + 3) / (2 * 4; // DOES NOT COMPILE
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.
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.