Java Operators and Expressions Study Notes

Arithmetic Operators

  • Perform mathematical operations on operands:

    • Addition (++): Adds operands (x=y+zx = y + z).

    • Subtraction (−-): Subtracts right operand from left operand (x=y−zx = y - z).

    • Multiplication (∗*): Multiplies operands (x=y∗3x = y * 3).

    • Division (//): Divides left operand by right operand (x=y/zx = y / z).

    • Modulus (%\%): Calculates remainder of integer division (x=y%zx = y \% z).

  • Primitive numeric operations automatically convert calculations to int\text{int} if exceeding the byte range of −128-128 to +127+127.

  • Joining string operands with ++ concatenates them into a single string (e.g., 2+"abc"="2abc"2 + \text{"abc"} = \text{"2abc"}).

Assignment Operators

  • Assign values, variables, or functions to variables:

    • Simple assignment (==): Assigns right operand value to left operand (x=yx = y).

    • Compound assignments: +=+=, −=-=, ∗=*=, /=/=, %=\%= (e.g., x+=yx += y is equivalent to x=x+yx = x + y).

  • Allows simultaneous assignment to multiple variables (e.g., x=y=0x = y = 0).

Unary Operators

  • Require a single operand to execute operations:

    • Unary Plus (++) and Unary Minus (−-): Represent positive (+a+a) or negative (−a-a) values.

    • Increment (++++) and Decrement (−−--): Increase or decrease operand value by 11.

    • Pre-increment/decrement (e.g., ++invar++invar): Modifies value prior to evaluation (e.g., if x=7x = 7, ++x++x becomes 88).

    • Post-increment/decrement (e.g., invar−−invar--): Modifies value after evaluation (e.g., if x=12x = 12, x−−x-- becomes 1111).

    • Logical Complement (!!): Inverts a Boolean value (e.g., !true!\text{true}).

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 (00) as fillers:

    • Right Shift (>>>>): Shifts bits right by specified second operand value (e.g., 9>>2=29 >> 2 = 2).

    • Left Shift (<<<<): Shifts bits left, filling zeros on the right (e.g., 9<<2=369 << 2 = 36).

    • Right Unsigned Shift (>>>>>>): Shifts bits right and replaces shifted bits with 00 (e.g., −10>>>3-10 >>> 3).

Bitwise Operators

  • Perform bit-by-bit operations on byte\text{byte}, short\text{short}, int\text{int}, long\text{long}, and char\text{char} data types:

    • AND (&\&): Yields 11 if both bits are 11, otherwise 00.

    • OR (∣|): Yields 00 if both bits are 00, otherwise 11.

    • XOR (\text{^}): Yields 00 if bits are identical, 11 if different.

    • Inversion (∼\sim): Converts all 11 bits to 00 and 00 bits to 11.

Logical Operators

  • Combine outcomes of Boolean expressions:

    • Logical AND (&&\&\&): Returns true\text{true} if both operands are non-zero/true; otherwise returns false\text{false}.

    • Logical OR (∣∣||): Returns true\text{true} if at least one operand is non-zero/true; otherwise returns false\text{false}.

Conditional Operator

  • Ternary Operator (condition?val1:val2\text{condition} ? \text{val1} : \text{val2}): Evaluates val1\text{val1} if condition returns true, otherwise evaluates val2\text{val2} (e.g., x=(y>z)?y:zx = (y > z) ? y : z).

Order of Precedence

  • Hierarchy defining execution order from highest to lowest precedence:

    1. High Precedence: [][], ()()

    2. Unary: ++, −-, ∼\sim, !!, ++++, −−--

    3. Multiplicative: ∗*, //, %\%

    4. Additive: ++, −-

    5. Shift: <<<<, >>>>, >>>>>>

    6. Relational: <<, <=<=, >=>=, >>

    7. Equality: ====, !=!=

    8. Bitwise: &\&, \text{^}, ∣|

    9. Logical: &&\&\&, ∣∣||

    10. Conditional: ?:?:

    11. 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., int z=x+yint\ z = x + y).

    • Assignment Expression: Assigns a value to a variable (e.g., a=a+4a = a + 4).

    • Relational Expression: Compares values using relational operators (e.g., boolean result=x<yboolean\ result = x < y).

    • Logical Expression: Evaluates multiple conditions with logical operators (e.g., boolean result=(j<k)&&(k<4)boolean\ result = (j < k) \&\& (k < 4)).

    • Conditional Expression: Uses the ternary operator to assign values based on evaluated conditions (e.g., int n=(l<m)?2:3int\ n = (l < m) ? 2 : 3).