Chapter 3 Flashcards
What is the lowest tier of operator in terms of precedence.
Assignment operators
=, +=, -=, *=, /=, %=, &=, ^=, |=, «=,»_space;-,»_space;>-
What are the 3 highest tier of operators in terms of precedence.
Post-unary operators, var++, var–
Pre-unary operators, ++var, –var
Other unary operators, -, !, ~, + (type)
how many variables or operands does a unery operator require.
one.
List all the operator types in order (12 in total)
Post-unary operators, var++, var-- Pre-unary operators, ++var, --var Other unary operators, -, !, ~, + (type) Multiplication/division/modulus, *, /, % Addition/subtraction, +, - Shift operators <>, >>> Relation operators , <=, >=, instanceof Equal to/not equel to ==, != Logical operators &, ^, | Short-circuit logical operators &&, || Ternary operators, boolean expression Assignment operators =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>-, >>>-
what does the following unery operator do : !
Flips a boolean value. !False() = true() and vice versa.
what does the following unery operator do : -
it forces a numeric value into the negative.
what is the value of : A if B is 3.
int A = ++B * 5 / B–;
a = 5
First the post-unery operator is triggered.
setting B To 2.
Then the pre-unery operator is triggered
Setting B to 3
the equation is then evaluated left to right
3 * 5 / 3 =
list all the unary operators
! flips a boolean \+ force a number to a positive value - force a number to a negative value \++ increments a value by 1 -- decrements a value by 1 (type) casts a value to a specific type
list all the binary operators
\+ Adds two values - subtracts two numeric values * multiplies two numeric values / devides one numeric value by another % modules returns the remainder after division of one numeric value by another.
what happens when two values have different numeric data types
Java will promote the lower value to the higher value. So int + long = long or byte + int = int
what happens when a integral value is part of a operation with a floating value.
the integral value is treated as the floating value.
int + double = double
int + float = float
what happens to a byte, a short or a char when it is part of a binary operations.
They are promoted to ints.
byte + byte = int
byte + char = int
short + char = int
what does this assignment operator do : =
it assigns a value to a variable.
so
var = (complex method)
var = (result of complex method)
what happens when we use casting on values in java.
It forces the output of a numeric operation into a certain value. var A = short + short A = int because of numeric promotion rules. but var B = (short) (short + short) B = short because of casting. short C = short + short //Does not compile Because the compiler knows that the numeric promotion rules forces the outcome into a int. Casting is required here to allow the operation to compile.
when is it required to use casting in java
When ever the code goes from a larger numerical data type to a smaller numerical data type.