Java Operators Flashcards
Arithmetic Operators in Java are used for what purpose?
Arithmetic Operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
Example: 10 % 3 = 1
What is the result when dividing two integers in Java?
When dividing two integers in Java, the result is an integer (fractional part is discarded).
Example: 10 / 3 = 3
What is the purpose of the Modulus operator in Java?
The Modulus operator in Java returns the remainder of a division.
Example: 10 % 3 = 1
What do Increment/Decrement Operators do in Java?
Increment/Decrement Operators in Java are used to increase or decrease a value by 1.
Example: int x = 10; int y = x++; // y = 10, x = 11
What are Relational Operators used for in Java?
Relational Operators in Java are used to compare two values or expressions and return a boolean result (true
or false
).
Example: boolean result1 = (a == b); // false
What is the main purpose of Logical Operators in Java?
The main purpose of Logical Operators in Java is to perform logical operations, primarily for combining multiple conditions.
Example: if (a > 0 && b > 0) { System.out.println("Both a and b are positive."); }
What does the Logical AND operator (&&
) do?
Returns true
if both conditions are true
.
The condition evaluates to true
only if both a > 0
and b > 0
are true.
What does the Logical OR operator (||
) do?
Returns true
if at least one condition is true
.
The condition evaluates to true
if either a > 0
or b > 0
is true.
What does the Logical NOT operator (!
) do?
Reverses the logical state of its operand. If a condition is true
, !
makes it false
, and vice versa.
If a > 0
is true
, then !(a > 0)
will be false
.
What is Short-circuit Evaluation in Java?
Java supports short-circuiting with logical operators. This means that evaluation of a logical expression stops as soon as the outcome is determined:
- For &&
, if the first condition is false
, the whole expression is false
, so the second condition is not evaluated.
- For ||
, if the first condition is true
, the whole expression is true
, so the second condition is not evaluated.
How do Relational and Logical Operators work together in Java?
Relational and logical operators often work together to form complex conditional statements in programs.
In this case:
- a < 15
is true
.
- b > 15
is true
.
- The &&
operator ensures both conditions are true, so the statement inside the if
block will execute.
What is Operator Precedence in Java?
Java evaluates operators based on a predefined operator precedence. Operators with higher precedence are evaluated first. For example, arithmetic operators generally have higher precedence than relational operators, which in turn have higher precedence than logical operators.
In this case, multiplication *
is performed before addition +
.