https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/QandE/answers_operators.html
https://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i
Operators | Precedence |
---|---|
postfix | expr++ expr-- |
unary | ++expr --expr +expr -expr ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
++i
will increment the value ofi
, and then return the incremented value.i = 1; j = ++i; (i is 2, j is 2)
i++
will increment the value ofi
, but return the original value thati
held before being incremented.i = 1; j = i++; (i is 2, j is 1)
- Consider the following code snippet:
arrayOfInts[j] > arrayOfInts[j+1]
Answer:>
,+
- Consider the following code snippet:
int i = 10; int n = i++%5;
- Question: What are the values of
i
andn
after the code is executed?
Answer:i
is 11, andn
is 0. - Question: What are the final values of
i
andn
if instead of using the postfix increment operator (i++
), you use the prefix version (++i)
)?
Answer:i
is 11, andn
is 1.
- Question: What are the values of
- Question: To invert the value of a
boolean
, which operator would you use?
Answer: The logical complement operator "!". - Question: Which operator is used to compare two values,
=
or==
?
Answer: The==
operator is used for comparison, and=
is used for assignment. - Question: Explain the following code sample:
result = someCondition ? value1 : value2;
Answer: This code should be read as: "IfsomeCondition
istrue
, assign the value ofvalue1
toresult
. Otherwise, assign the value ofvalue2
toresult
."
No comments:
Post a Comment