TYPES OF OPERATORS
OPERATOR PRECEDENCE
expression++, expression--++expression, --expression-, !, ~, +, (type)*, /, %+, -<<, >>, >>><, >, <=, >=, instanceof==, !=&, ^, |&&, ||boolean expression ? expression1 : expression2=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=LOGICAL COMPLEMENT AND NEGATION OPERATORS
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
long pigeon = 1 + ((3 * 5) / 3; int blueJay = (9 + 2) + 3) / (2 * 4; short robin = 3 + [(4 * 2) + 4];
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
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
What is the data type of w * x / y?
short w = 14; float x = 13; double y = 30; var z = w * x / y;
double
CASTING VALUE
Casting is optional and unnecessary when converting to a larger or widening data type, but it is required when converting to a smaller or narrowing data type.
Examples of casting:
1: int fur = (int)5; 2: int hair = (short) 2; 3: String type = (String) "Bird"; 4: short tail = (short)(4 + 10); 5: long feathers = 10(long);
Why none of the following lines of code compile?
float egg = 2.0 / 9; // DOES NOT COMPILE int tadpole = (int)5 * 2L; // DOES NOT COMPILE short frog = 3 - 2.0; // DOES NOT COMPILE
All of these examples involve putting a larger value into a smaller data type.
Reviewing Primitive Assignments
int fish = 1.0; // DOES NOT COMPILE short bird = 1921222; // DOES NOT COMPILE int mammal = 9f; // DOES NOT COMPILE long reptile = 192301398193810323; // DOES NOT COMPILE
Fix these statements:
int fish = 1.0; // DOES NOT COMPILE short bird = 1921222; // DOES NOT COMPILE int mammal = 9f; // DOES NOT COMPILE long reptile = 192301398193810323; // DOES NOT COMPILE
int fish = (int)1.0; //cast double to int and stored as 1 short bird = (short)1921222; // cast int to short and stored as 20678 int mammal = (int)9f; // cast float to int long reptile = 192301398193810323L; //add postfix L
OVERFLOW AND UNDERFLOW
Overflow is when a number is so large that it will no longer fit within the data type, so the system “wraps around” to the lowest negative value and counts up from there, similar to how modulus arithmetic works.
There’s also an analogous underflow, when the number is too low to fit in the data type, such as storing -200 in a byte field.
System.out.print(2147483647+1); // -2147483648
Since 2147483647 is the maximum int value, adding any strictly positive value to it will cause it to wrap to the smallest negative number.
Why line 3 and 4 does not compile?
1: short mouse = 10; 2: short hamster = 3; 3: short capybara = (short)mouse * hamster; // DOES NOT COMPILE 4: short gerbil = 1 + (short)(mouse * hamster); // DOES NOT COMPILE
COMPOUND ASSIGNMENT OPERATORS
int camel = 2, giraffe = 3; camel = camel * giraffe; // Simple assignment operator camel *= giraffe; // Compound assignment operator