Chapter 3 Operators Notes Flashcards
TYPES OF OPERATORS
- unary
- binary
- ternary
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
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=
LOGICAL COMPLEMENT AND NEGATION OPERATORS
- logical complement operator (!) flips the value of a boolean expression.
- negation operator, -, reverses the sign of a numeric expression
Examples:
int pelican = !5; // DOES NOT COMPILE boolean penguin = -true; // DOES NOT COMPILE boolean peacock = !0; // DOES NOT COMPILE
1 and true
In Java, 1 and true are not related
in any way, just as 0 and false are not related
.
int lion = 3; int tiger = ++lion * 5 / lion--; System.out.println("lion is " + lion); System.out.println("tiger is " + tiger);
Printed:
lion is 3 tiger is 5
arithmetic operators
All of the arithmetic operators may be applied to any Java primitives, with the exception of boolean. Furthermore, 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.
Verifying Parentheses Syntax
- balanced parentheses
- left-parenthesis matching right-parenthesis.
long pigeon = 1 + ((3 * 5) / 3; int blueJay = (9 + 2) + 3) / (2 * 4; short robin = 3 + [(4 * 2) + 4];
- The first example does not compile because the parentheses are not balanced. There is a left-parenthesis with no matching right-parenthesis.
- The second example 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.
- The last example does not compile because Java, unlike some other programming languages, does not allow brackets, [], to be used in place of parentheses. If you replace the brackets with parentheses, the last example will compile just fine.
Division and Modulus Operators
System.out.println(9 / 3); // 3 System.out.println(9 % 3); // 0 System.out.println(10 / 3); // 3 System.out.println(10 % 3); // 1 System.out.println(11 / 3); // 3 System.out.println(11 % 3); // 2 System.out.println(12 / 3); // 4 System.out.println(12 % 3); // 0
jshell> 0/1 $17 ==> 0 jshell> 0/2 $18 ==> 0 jshell> 0 % 1 $19 ==> 0 jshell> 0 % 3 $20 ==> 0 jshell> 1 / 0 | Exception java.lang.ArithmeticException: / by zero | at (#21:1) jshell> 1 % 0 | Exception java.lang.ArithmeticException: / by zero | at (#22:1)
Exception java.lang.ArithmeticException: / by zero
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.
test
Numeric Promotion Rules
- If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
- 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.
- 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.
- 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.
What is the data type of x * y?
int x = 1; long y = 33; var z = x * y;
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!
Floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f.
What is the data type of x * y?
short x = 10; short y = 3; var z = x * y;
int