Java Operators and Expressions Study Notes
Arithmetic Operators
Perform mathematical operations on operands:
Addition (
): Adds operands ().Subtraction (
): Subtracts right operand from left operand ().Multiplication (
): Multiplies operands ().Division (
): Divides left operand by right operand ().Modulus (
): Calculates remainder of integer division ().
Primitive numeric operations automatically convert calculations to
if exceeding the byte range of to .Joining string operands with
concatenates them into a single string (e.g., ).
Assignment Operators
Assign values, variables, or functions to variables:
Simple assignment (
): Assigns right operand value to left operand ().Compound assignments:
,,,,(e.g., is equivalent to ).
Allows simultaneous assignment to multiple variables (e.g., ).
Unary Operators
Require a single operand to execute operations:
Unary Plus (
) and Unary Minus (): Represent positive () or negative () values.Increment (
) and Decrement (): Increase or decrease operand value by .Pre-increment/decrement (e.g.,
): Modifies value prior to evaluation (e.g., if ,becomes ).Post-increment/decrement (e.g.,
): Modifies value after evaluation (e.g., if ,becomes ).Logical Complement (
): Inverts a Boolean value (e.g.,).
Comparison Operators
Relational operators evaluate conditions between values and return Boolean outputs:
Equal to (
) and Not Equal to ()Greater Than (
) and Less Than ()Greater Than or Equal to (
) and Less Than or Equal to ()
Shift Operators
Shift bit patterns of integer data types using zeros () as fillers:
Right Shift (
): Shifts bits right by specified second operand value (e.g., ).Left Shift (
): Shifts bits left, filling zeros on the right (e.g., ).Right Unsigned Shift (
): Shifts bits right and replaces shifted bits with (e.g., ).
Bitwise Operators
Perform bit-by-bit operations on
,,,, anddata types:AND (
): Yields if both bits are , otherwise .OR (
): Yields if both bits are , otherwise .XOR (
\text{^}): Yields if bits are identical, if different.Inversion (
): Converts all bits to and bits to .
Logical Operators
Combine outcomes of Boolean expressions:
Logical AND (
): Returnsif both operands are non-zero/true; otherwise returns.Logical OR (
): Returnsif at least one operand is non-zero/true; otherwise returns.
Conditional Operator
Ternary Operator (
): Evaluatesif condition returns true, otherwise evaluates(e.g., ).
Order of Precedence
Hierarchy defining execution order from highest to lowest precedence:
High Precedence:
,Unary:
,,,,,Multiplicative:
,,Additive:
,Shift:
,,Relational:
,,,Equality:
,Bitwise:
,\text{^},Logical:
,Conditional:
Assignment:
,,,,,
Expressions in Java
Combination of variables, constants, literals, and operators evaluated to produce a single value, ending with a semicolon (
;).Types of Expressions:
Arithmetic Expression: Returns a numeric value based on operators and operands (e.g., ).
Assignment Expression: Assigns a value to a variable (e.g., ).
Relational Expression: Compares values using relational operators (e.g., ).
Logical Expression: Evaluates multiple conditions with logical operators (e.g., ).
Conditional Expression: Uses the ternary operator to assign values based on evaluated conditions (e.g., ).