Java Operators Explained: Types & Examples
Java Operators Explained: Types & Examples
Shift operators in Java manipulate the bits of a variable’s binary representation. The left shift operator (<<) shifts bits to the left, effectively multiplying the number by 2 to the power of the number of positions shifted. For example, '10<<2' results in 40. The right shift operator (>>) shifts bits to the right, dividing the number by 2 raised to the power of the number of positions shifted. '20>>2' results in 5 .
The assignment operator in Java assigns the value on the right side of the operator to the variable on the left side. When dealing with different data types, particularly primitives, implicit type conversion may occur. An explicit type cast is required when assigning a larger data type to a smaller one. For instance, 'short a = 10; short b = 10; a = (short) (a+b);' is necessary to avoid a compilation error when the result of 'a + b' (automatically an int) is assigned back to a short .
The ternary operator in Java acts as a succinct alternative to if-then-else statements, using three operands. The syntax is: 'condition ? expressionIfTrue : expressionIfFalse'. For example, 'int min = (a < b) ? a : b;' assigns the value of 'a' to 'min' if 'a' is less than 'b', otherwise 'b' .
Both logical OR (||) and bitwise OR (|) operators in Java evaluate boolean expressions. The key difference is that || is a short-circuit operator, meaning it evaluates the second operand only if the first is false. In contrast, | checks both operands regardless. For example, 'a>b||a++<c' checks only the first condition if true, leaving 'a' unchanged if it short-circuits, whereas 'a>b|a++<c' evaluates both, incrementing 'a' .
Arithmetic operators in Java enable computations by performing basic mathematical operations: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). In complex expressions, they combine to produce a computed result. For example, '10*10/5+3-1*4/2' uses multiple arithmetic operators to yield '21'. This involves applying operator precedence rules, evaluating multiplication and division before addition and subtraction .
Relational operators in Java are used to compare two values. They include '==', '!=', '>', '<', '>=', and '<='. For example, '==' checks if two values are equal, while '!=' checks for inequality. The operator '>' tests if the left operand is greater than the right, while '<' checks if it is less. '>=' and '<=' check for equality in addition to greater or less, respectively. Each operator returns a boolean result based on the comparison .
Unary operators in Java operate on a single operand to perform various operations such as incrementing/decrementing a value, negating an expression, or inverting a boolean value. Examples include increment (++), decrement (--), bitwise complement (~), and logical complement (!). In the first example, ++ and -- are used to adjust a numeric value. For instance, if 'int x = 10;', 'x++' outputs 10 (but increments x to 11), and '++x' outputs 12 .
Operator precedence in Java determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before those with lower precedence, akin to the order of operations in mathematics. For instance, in the expression '10*10/5+3-1*4/2', multiplication and division are evaluated before addition and subtraction, resulting in a final output of 21. Parentheses can be used to explicitly specify evaluation order .
Compound assignment operators merge an operation and an assignment, like '+=', '-=', '*=', '/=', '%=', etc. They provide a shorthand to apply an operation to a variable and assign the result back to that variable. These operators can trigger implicit type conversion; if operands are of a different type, the result type may convert automatically to match the type of the left operand. For instance, 'a+=b' involves first converting the types to match and then executing the operation .
The logical AND (&&) operator in Java is a short-circuiting operator, meaning it evaluates the second operand only if the first operand is true. Conversely, the bitwise AND (&) does not short-circuit; it always evaluates both operands. For example, given 'int a=10; int c=20;', evaluating 'a<b&&a++<c' results in false, but 'a' remains 10 because the second condition is not evaluated. However, 'a<b&a++<c' also results in false, but 'a' becomes 11 as the increment operation is executed .