2 Operators and Statements Flashcards
Numeric Promotion Rule 1
- If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
Numeric Promotion Rule 2
- 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.
Numeric Promotion Rule 3
- 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.
For the third rule, note that unary operators are excluded from this rule.
For example, applying ++ to a short value results in a short value
Numeric Promotion Rule 4
- 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;
If we follow the first rule, since one of the values is long and the other is int, and long is larger than int, then the int value is promoted to a long, and the resulting value is long.
What is the data type of x + y?
double x = 39.21;
float y = 2.1;
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.
This means that you have two ways of initialising floats.
either with an integral number or a floating point with f/F in the end, both valid.
First one because an int is promoted to floating point.
So float y = 2.1; // Wont compile
If the value was set properly to 2.1f, then the promotion would be similar to the last example, with both operands being promoted to a double, and the result would be a double value, because double olds 64 bits while float olds 32 bits.
What is the data type of x / y?
short x = 10;
short y = 3;
In this case, we must apply the third rule, namely that x and y will both be promoted to int before the operation, resulting in an output of type int.
Pay close attention to the fact that the resulting output is not a short.
What is the data type of x * y / z?
short x = 14;
float y = 13;
double z = 30;
In this case, we must apply all of the rules. First, x will automatically be promoted to int solely because it is a short and it is being used in an arithmetic binary operation.
The promoted x value will then be automatically promoted to a float so that it can be multiplied with y.
The result of x * y will then be automatically promoted to a double, so that it can be multiplied with z, resulting in a double value.
Working with Unary Operators
Working with Unary Operators
What is an unuary operator?
A unary operator is one that requires exactly one operand, or variable, to function.
They often perform simple tasks, such as increasing a numeric variable by one, or negating a boolean value.
Which unuary operators exist in Java?
+, - , ++ , – , !
+ -> Indicates a number is positive, although numbers are assumed to be positive in Java unless accompanied by a negative unary operator.
- (minus) Indicates a literal number is negative or negates an expression
++ Increments a value by 1
– Decrements a value by 1
! Inverts a Boolean’s logical value
Increment and Decrement Operators
Increment and Decrement Operators
Common practice in a certification exam, question
int x = 3;
int y = ++x * 5 / x– + –x;
System.out.println(“x is “ + x);
System.out.println(“y is “ + y);
So how do you read this code?
First, the x is incremented and returned to the expression, which is multiplied by 5.
We can simplify this:
int y = 4 * 5 / x– + –x; // x assigned value of 4
Next, x is decremented, but the original value of 4 is used in the expression, leading to
this:
int y = 4 * 5 / 4 + –x; // x assigned value of 3
The final assignment of x reduces the value to 2, and since this is a pre-increment opera-
tor, that value is returned to the expression:
int y = 4 * 5 / 4 + 2; // x assigned value of 2
Finally, we evaluate the multiple and division from left-to-right, and finish with the addi- tion. The result is then printed:
x is 2
y is 7
Assignment Operators
An assignment operator is a binary operator that modifies, or assigns, the variable on the left-hand side of the operator, with the result of the value on the right-hand side of the equation.
Can larger dat types be assigned to smaller datatypes?
Java will automatically promote from smaller to larger data types, as we saw in the pre- vious section on arithmetic operators, but it will throw a compiler exception if it detects you are trying to convert from larger to smaller data types.
int x = 1.0; // DOES NOT COMPILE ( double 64 bits to int 32 bits)
short y = 1921222; // DOES NOT COMPILE (is outside of
the range of short which is-32,768 to 32,767)
int z = 9f; // DOES NOT COMPILE ( floating-point value)
long t = 192301398193810323; // DOES NOT COMPILE
(because the declaration doesn’t have L in the end, java interprets the long as an int nd notices that the value is larger than int allows )
In Java, there are two types of casting..
Whaats the order
Widening Casting (automatically) - converting a smaller type to a larger type size: byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type double -> float -> long -> int -> char -> short -> byte
What would be the resulting type of this operation?
short x = 10;
short y = 3;
short z = x * y;
short z = x * y; // DOES NOT COMPILE
Smaller types are automatically casted to int, so x and y will become ints.
Following the rules a larger type does not fit in a smaller type.
To make this compile an explicit casting to short would have to be made:
short z = (short)(x * y);
Compound Assignment Operators
Only two of the compound operators listed in Table 2.1 are required for the exam, += and -=.
x = x * z; // Simple assignment operator
x *= z; // Compound assignment operator
The left-hand side of the compound operator can only be applied to a variable that is already defined and cannot be used to declare a new variable. In the previous example, if x was not already defined, then the expression x *= z would not compile.
What’s the alternative for this operation that will makes avoid an explicit casting?
long x = 10;
int y = 5;
y = y * x; //DOES NOT COMPILE
Compound operators are useful for more than just shorthand—they can also save us from having to explicitly cast a value.
long x = 10;
int y = 5;
y = y * x; // DOES NOT COMPILE
This last line could be fixed with an explicit cast to (int), but there’s a better way using the compound assignment operator:
long x = 10;
int y = 5;
y *= x;
The compound operator will first cast x to a long, apply the multiplication of two long values, and then cast the result to an int.
One final thing to know about the assignment operator is that the result of the assign- ment is an expression in and of itself, equal to the value of the assignment.
long x = 5;
long y = (x=3);
System.out.println(x); // Outputs 3
System.out.println(y); // Also, outputs 3
For example, the following snippet of code is perfectly valid, if not a little odd looking:
long x = 5;
long y = (x=3);
System.out.println(x); // Outputs 3
System.out.println(y); // Also, outputs 3
The key here is that (x=3) does two things.
First, it sets the value of the variable x to be 3.
Second, it returns a value of the assignment, which is also 3.
The exam creators are fond of inserting the assignment operator = in the middle of an expression and using the value of the assignment as part of a more complex expression.
Relational operators
< Strictly less than
<= Less than or equal to
> Strictly greater than
> = Greater than or equal to
instanceof rules
1. The instanceof operator cannot tell you the exact type of the object being pointed to by a variable. It can only tell you whether that object is-a something. For example, if you do finstanceof Fruit, it will return true if the object referred to by f is-a Fruit, which means it will return true even if f points to a Mango because a Mango is-a Fruit. In the case of interfaces, it will return true if the class of the object pointed to by the reference implements the given interface (directly or indirectly).
- The compiler will let you use instanceof operator only if it is possible for the variable to refer to an object of the type given on the right-hand side, with exception for interfaces which are accepted even if there isno relation on the type.
For example, f instanceof Mango is valid because the compiler knows that the declared type of f is Fruit and since Mango is a Fruit, it is possible for f to point to a Mango. But f instanceof String will not compile because the compiler knows that there is no way f can ever point to a String. The instanceof operator behaves the same way as the cast operator in this respect.
Summing up:
when using instanceof operator pay attention to 2 details:
- The left operand of instanceof MUST be a reference variable and not a primitive.
- Right operand of instanceof MUST be a reference type name, i.e., a class, an interface, or an enum name.
- the class must have a is-a relation with the left-hand side object however if it is an interface the is-a relation is not mandatory - IMPORTANT
2 the instanceof can only tell you if an object is of a certain type, and returns a boolean, however keep in mind that
3 if the right hand side is an interface, even if there is no relation ( is-a ), it the class of the object doesn’t implements that interface, it will compile fine. and return false.
interface Flyer{ } class Bird implements Flyer {} class Eagle extends Bird{ } class Bat {}
Flyer f = new Eagle(); Eagle e = new Eagle(); Bat b = new Bat();
if(f instanceof Flyer) System.out.println("f is a Flyer"); if(e instanceof Bird) System.out.println("e is a bird"); if(b instanceof Flyer) System.out.println("bis a Flyer");