Operators and Statements Flashcards
What would the operation 10/3 output?
3
If two values have different data types and have an operator applied to them …
Java will automatically promote one of the values to the larger of the two data types
short a = 1;
int b = 1;
a+b will be an int
If one of the values is integral and the other is floating point and they have an operator applied to them …
Java will automatically promote the integral value to the floating-point value’s data type
int a = 1;
double b = 1.0;
a+b will be a double
When byte, short, and char are involved in a binary operation …
They are promoted to int before operation, even if neither operand is an int
short a = 10;
byte b = 5;
a/b will be an int
What is the output?
int counter = 0; System.out.println(counter); System.out.println(++counter); System.out.println(counter); System.out.println(counter--); System.out.println(counter);
0 1 1 1 0
x & y
booleans
Result same as x && y
x | y
booleans
Result same as x || y
x ^ y
booleans
Exclusive or
With x || y and x && y if x is true (for ||) or x is false (for && y) then …
y is not evaluated
Note: not the case for | and &
Two references are equal if and only if ….
They point to the same object or both point to null
What are the boolean values of 0 and 1?
Nothing! They mean nothing in the boolean world of Java!
booleanExp ? exp1 : exp2
If booleanExp is true, then execute exp1, otherwise execute exp2
switch statement syntax
switch (variableToTest) { case constantExp: //Code to run break; default: //Code to run
What are the data types supported by switch statements?
1) byte and Byte
2) short and Short
3) char and Character
4) int and Integer
5) String
6) enum values
What are the three types of values you can use for case values in a switch statement?
A literal, enum constant, or a final constant variable