• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Java | Java Operators
  1. Notes
  2. Simple Assignment Operator
  3. Arithmetic Operators
  4. Unary Operators
  5. Equality and Relational Operators
  6. Conditional Operators
  7. Bitwise Operators
  8. Bit Shift Operators
  9. Type Comparison Operator
  10. Operator Precedence

  1. Notes
    Short-circuit evaluation: means that the right hand side of the expression won't be evaluated if it isn't necessary.
  2. Simple Assignment Operator
    =       // Simple assignment operator
  3. Arithmetic Operators
    +       // Additive operator (also used for strings concatenation)
    -       // Subtraction operator
    *       // Multiplication operator
    /       // Division operator
    %       // Remainder operator (Modulo operator)
  4. Unary Operators
    +       // Unary plus operator; indicates positive value
    -       // Unary minus operator; negates an expression
    
    ++      // Increment operator; increments a value by 1
    --      // Decrement operator; decrements a value by 1
    
    !       // Logical complement operator; inverts the value of a boolean
  5. Equality and Relational Operators
    ==      // Equal to
    !=      // Not equal to
    
    >       // Greater than
    >=      // Greater than or equal to
    
    <        // Less than
    <=       // Less than or equal to
  6. Conditional Operators
    &&      // Conditional-AND (short-circuit evaluation)
    ||      // Conditional-OR (short-circuit evaluation)
    ?:      // Ternary (shorthand for if-then-else statement)
  7. Bitwise Operators
    ~       // Unary bitwise complement
    
    &       // Bitwise AND (always evaluate both sides)
    |       // Bitwise inclusive OR (always evaluate both sides)
    
    ^       // Bitwise exclusive OR (always evaluate both sides)
    Java Samples:
    • Unary bitwise complement:
      final byte b1 = 0;      //  0: (0) 0000000
      final byte b1_ = ~b1;   // -1: (1) 1111111
      final byte b1__ = ~b1_; //  0: (0) 0000000
      
      final byte b2 = 1;      //  1: (0) 0000001
      final byte b2_ = ~b2;   // -2: (1) 1111110
      final byte b2__ = ~b2_; //  1: (0) 0000001
      
      final byte b3 = 127;    //  127: (0) 1111111
      final byte b3_ = ~b3;   // -128: (1) 0000000
      final byte b3__ = ~b3_; //  127: (0) 1111111
    • Bitwise AND:
      System.out.println("1 & 1: " + (1 & 1)); // output: 1 & 1: 1
      System.out.println("1 & 0: " + (1 & 0)); // output: 1 & 0: 0
      System.out.println("0 & 1: " + (0 & 1)); // output: 0 & 1: 0
      System.out.println("0 & 0: " + (0 & 0)); // output: 0 & 0: 0
    • Bitwise inclusive OR:
      System.out.println("1 | 1: " + (1 | 1)); // output: 1 | 1: 1
      System.out.println("1 | 0: " + (1 | 0)); // output: 1 | 0: 1
      System.out.println("0 | 1: " + (0 | 1)); // output: 0 | 1: 1
      System.out.println("0 | 0: " + (0 | 0)); // output: 0 | 0: 0
    • Bitwise exclusive OR:
      System.out.println("1 ^ 1: " + (1 ^ 1)); // output: 1 ^ 1: 0
      System.out.println("1 ^ 0: " + (1 ^ 0)); // output: 1 ^ 0: 1
      System.out.println("0 ^ 1: " + (0 ^ 1)); // output: 0 ^ 1: 1
      System.out.println("0 ^ 0: " + (0 ^ 0)); // output: 0 ^ 0: 0
  8. Bit Shift Operators
    <<        // Left shift
    <<=       // Left shift assignment
    
    >>        // Signed right shift
    >>=       // Signed right shift assignment
    
    >>>       // Unsigned right shift
    >>>=      // Unsigned right shift assignment
    Java Samples:
    • << left shift
      Shifts left by n positions.
      Fills with 0s from right.
      final byte b1 = 1;         // 1: (0) 0000001
      final byte b1_ = b1 << 1;  // 2: (0) 0000010
      final byte b1__ = b1 << 2; // 4: (0) 0000100
      final byte b1 = -1;        // -1: (1) 1111111
      final byte b1_ = b1 << 1;  // -2: (1) 1111110
      final byte b1__ = b1 << 2; // -4: (1) 1111100
    • <<= Left shift assignment
      final int b1 = 1;
      b1 <<= 1; // Equivalent to: b1 = b1 << 1;
    • >> Signed right shift
      Shifts right by n positions.
      Preserves the sign bit.
      For positive numbers, fills with 0s from left.
      For negative numbers, fills with 1s from left.
      final byte b1 = 1;        // 1: (0) 0000001
      final byte b1_ = b1 >> 1; // 0: (0) 0000000
      
      final byte b2 = 2;        // 2: (0) 0000010
      final byte b2_ = b2 >> 1; // 1: (0) 0000001
      
      final byte b3 = 4;        // 4: (0) 0000100
      final byte b3_ = b3 >> 1; // 2: (0) 0000010
      final byte b1 = -2;       // -2: (1) 1111110
      final byte b1_ = b1 >> 1; // -1: (1) 1111111
      
      final byte b2 = -4;       // -4: (1) 1111100
      final byte b2_ = b2 >> 1; // -2: (1) 1111110
      
      final byte b3 = -8;       // -8: (1) 1111000
      final byte b3_ = b3 >> 1; // -4: (1) 1111100
    • >>= Signed right shift assignment
      final int b1 = 1;
      b1 >>= 1; // Equivalent to: b1 = b1 >> 1;
    • >>> Unsigned right shift
      Shifts right by n positions.
      Fills with 0s from left.
      final byte b1 = 8;          // 1: (0) 0001000
      final byte b1_ = b1 >>> 1;  // 2: (0) 0000100
      final byte b1__ = b1 >>> 2; // 4: (0) 0000010
      final int b2 = -8;         // 1: (1) 1111111111111111111111111111000
      final int b2_ = b2 >>> 1;  // 2147483644: (0) 1111111111111111111111111111100
      final int b2__ = b2 >>> 2; // 1073741822: (0) 0111111111111111111111111111110
    • >>>= Unsigned right shift assignment
      final int b1 = 1;
      b1 >>>= 1; // Equivalent to: b1 = b1 >>> 1;
  9. Type Comparison Operator
    instanceof // Compares an object to a specified type
  10. Operator Precedence
    Associativity "Left to right": x = a / b * c can be written x = (a / b) * c
    Associativity "Right to left": a += b -= c can be written a += (b -= c)

    Operator Description Associativity
    []
    .
    ()
    Array element access
    Object member access
    Method call
    Left to right
    ++
    --
    Post increment
    Post decrement
    !
    ~
    ++
    --
    +
    -
    ()
    new
    Logical NOT
    Bitwise NOT
    Pre increment
    Pre decrement
    Unary plus
    Unary minus
    Cast
    Instantiation
    Right to left
    *
    /
    %
    Multiplication
    Division
    Remainder
    Left to right
    +
    -
    +
    Additive
    Subtraction
    String concatenation
    Left to right
    <<
    >>
    >>>
    Signed left shift
    Signed right shift
    Unsigned right shift
    Left to right
    <
    <=
    >
    >=
    instanceof
    Less than
    Less than or equal to
    Greater than
    Greater than or equal to
    Type Comparison
    Left to right
    ==
    !=
    Equal to
    Not equal to
    Left to right
    & Bitwise AND Left to right
    ^ Bitwise XOR Left to right
    | Bitwise OR Left to right
    && Conditional AND Left to right
    || Conditional OR Left to right
    ?: Ternary Right to left
    =
    +=
    -=
    *=
    /=
    %=
    &=
    |=
    ^=
    <<=
    >>=
    >>>=
    Right to left
© 2025  mtitek