Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
An operator is not necessarily a mathematical symbol between two numbers. It specifies an operation involving one or more operands, and an operand can be a variable or a literal. In score++, the operator is ++ and the operand is score. In score >= 8, the operator is >= and its operands are score and 8. In passed ? "pass" : "retry", the operator is ?: and its operands are passed, "pass", and "retry".
int score = 7;
score++;
boolean passed = score >= 8;
String status = passed ? "pass" : "retry";An operator can calculate a result, change a stored value, or do both through an expression used by an assignment. The ++ operator changes the value stored in score. The >= operator calculates a boolean result from score and 8. The ?: operator selects one of two String literals using passed.
In score >= 8, which part is the operator, and which parts are its operands?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
An operator's arity is the number of operands connected to it. The ++ in score++ uses one operand, so it is unary. The >= in score >= 8 uses two operands, so it is binary. The ?: in passed ? "pass" : "retry" uses three operands, so it is ternary.
The punctuation used to write an operator does not determine its arity. The ternary operator is one operator written with two punctuation marks, ? and :. Together they form ?: and connect passed with the two possible String operands, "pass" and "retry".
Start with score stored as 7. The statement score++ changes the stored value to 8. Next, score >= 8 uses the current score and the literal 8, producing the boolean value true. The assignment operator = stores that result in the variable passed, so passed becomes true.
int score = 7; // score stores 7
score++; // score now stores 8
boolean passed = score >= 8; // >= produces true, then = stores it
String status = passed ? "pass" : "retry"; // ?: produces "pass"The final expression uses passed as its first operand. Because passed is true, ?: produces the String value "pass" instead of "retry". The assignment operator stores that value in status. The finished values are score equal to 8, passed equal to true, and status equal to "pass".
Enter the arities of ++, >=, and ?: in that order.
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The running statements contain several symbols that do different jobs. The operators are ++, >=, =, and ?: because each specifies an operation involving operands. The spaces separate parts of the statement, and the semicolon marks its end. String is a data type that describes status, not an operator. The quotation marks around "pass" and "retry" mark the boundaries of String literals, so they are not operators either.
Use one reliable test when a symbol seems unfamiliar: ask whether it has operands and specifies an operation involving them. In score >= 8, >= passes this test because it acts on score and 8. In status = ..., = passes it because it stores a value in status. A semicolon has no operands and does not perform an operation, so it is punctuation instead.