Programmer I Chapter 3: Operators Flashcards
name 4 Numeric promotion rules
- If two values have different datatypes, 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;
If we follow the first rule, since one of the values is long and the other is int and since 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; var z = x + y;
This is actually a trick question, as this code will not compile! As you may remember from Chapter 2, floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f. If the value of y was set properly to 2.1f, then the promotion would be similar to the previous example, with both operands being promoted to a double, and the result would be a double value.
What is the data type of x * y? short x = 10; short y = 3; var z = x * y;
On the last line, we must apply the third rule, namely, that x and y will both be promoted to int before the binary multiplication operation, resulting in an output of type int. If you were to try to assign the value to a short variable without casting, the code would not compile. Pay close attention to the fact that the resulting output is not a short, as we’ll come back to this example in the upcoming “Assigning Values” section.
What is the data type of w * x / y? short w = 14; float x = 13; double y = 30; var z = w * x / y;
In this case, we must apply all of the rules. First, w will automatically be promoted to int solely because it is a short and it’s being used in an arithmetic binary operation. The promoted w value will then be automatically promoted to a float so that it can be multiplied with x. The result of w * x will then be automatically promoted to a double so that it can be divided by y, resulting in a double value.
what lines compile?
float egg = 2.0 / 9;
int tadpole = (int)5 * 2L;
short frog = 3 - 2.0;
none of them
what line causes a compile error?
short mouse = 10;
short hamster = 3;
short capybara = mouse * hamster;
3rd one.
1 and 2 are fine despite the fact that literals are of int type - Java allows for implicit compile-time narrowing of constants.
A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
does this compile?
long goat = 10;
int sheep = 5;
sheep *= goat;
Yes.
The compound operator will first cast sheep to a long, apply the multiplication of two long values, and then cast the result to an int.
what will this print?
long wolf = 5;
long coyote = (wolf=3);
System.out.println(wolf);
System.out.println(coyote);
3
3
Assignment expressions are legal
what will this print?
System.out.print(null instanceof Object);
Object noObjectHere = null;
System.out.print(noObjectHere instanceof String);
System.out.print(null instanceof null);
does not compile.
calling instanceof on the null literal or a null reference always returns false. The last line does not compile, since null is used on the right side of the instanceof operator
what is the output?
int rabbit = 6;
boolean bunny = (rabbit >= 6) || (++rabbit <= 7);
System.out.println(rabbit);
Because rabbit >= 6 is true, the increment operator on the right side of the expression is never evaluated, so the output is 6.
what is the output?
int sheep = 1;int zzz = 1;
int sleep = zzz<10 ? sheep++ : zzz++;
System.out.print(sheep+”,”+zzz);
2,1
since the left-hand boolean expression was true, onlysheep was incremented
Which of the following Java operators can be used with booleanvariables? (Choose all that apply.)
A. == B. + C. -- D. ! E. % F. <= G. Cast with (boolean)
A, D, G. Option A is the equality operator and can be used onprimitives and object references. Options B and C are botharithmetic operators and cannot be applied to a boolean value.Option D is the logical complement operator and is usedexclusively with boolean values. Option E is the modulus operator,which can be used only with numeric primitives. Option F is arelational operator that compares the values of two numbers.Finally, option G is correct, as you can cast a boolean variablesince boolean is a type.
What data type (or types) will allow the following code snippet tocompile? (Choose all that apply.)
byte apples = 5;
short oranges = 10;
_______ bananas = apples + oranges;
A. int B. long C. boolean D. double E. short F. byte
A, B, D. The expression apples + oranges is automaticallypromoted to int, so int and data types that can be promotedautomatically from int will work. Options A, B, and D are suchdata types. Option C will not work because boolean is not anumeric data type. Options E and F will not work without anexplicit cast to a smaller data type.
What change, when applied independently, would allow the following code snippet to compile? (Choose all that apply.)
3: long ear = 10;
4: int hearing = 2 * ear;
A. No change; it compiles as is.
B. Cast ear on line 4 to int.
C. Change the data type of ear on line 3 to short.
D. Cast 2 * ear on line 4 to int.
E. Change the data type of hearing on line 4 to short.
F. Change the data type of hearing on line 4 to long.
B, C, D, F. The code will not compile as is, so option A is not correct. The value 2 * ear is automatically promoted to long and cannot be automatically stored in hearing, which is in an int value. Options B, C, and D solve this problem by reducing the long value to int. Option E does not solve the problem and actually makes it worse by attempting to place the value in a smaller datatype. Option F solves the problem by increasing the data type of the assignment so that long is allowed.