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 | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
++iwill 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 thatiheld before being incremented.i = 1; j = i++; (i is 2, j is 1)
- Consider the following code snippet:Question: What operators does the code contain?
arrayOfInts[j] > arrayOfInts[j+1]
Answer:>,+ - Consider the following code snippet:
int i = 10; int n = i++%5;
- Question: What are the values of
iandnafter the code is executed?
Answer:iis 11, andnis 0. - Question: What are the final values of
iandnif instead of using the postfix increment operator (i++), you use the prefix version (++i))?
Answer:iis 11, andnis 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: "IfsomeConditionistrue, assign the value ofvalue1toresult. Otherwise, assign the value ofvalue2toresult."
No comments:
Post a Comment