Page 349 - Computer_Science_F5
P. 349

Computer Science  (h)  Unary operator: This includes Postfix: expr++ expr-- and Prefix: ++expr,

                                 --expr,  +expr, and  -expr as shown in Table 5.13.


           Table 5.13: Unary operator
            Operator Description
                                            Example
                                                       Explanation
          FOR ONLINE READING ONLY
            +
                        Unary plus
                         operator (promotes    +5      No effect on the value; it is typically
                                                       used to explicitly indicate a positive
                        the value)                     number.
            -           Unary minus             -5     Negates the value, changing positive
                        operator (negates              numbers to negative and vice versa.
                        the value)
            ++          Increment operator   ++a or    Pre-increment (++a) increases value
                        (increases the         a++     before use; post-increment  (a++)
                        value by 1)                    increases value after use.

            --          Decrement           --a or a-- Pre-decrement (--a) decreases value
                        operator (decreases            before  use; post-decrement  (a--)
                        value by 1)                    decreases value after use.

            !           Logical               !true    Inverts the boolean value, changing
                        complement                     true to false and vice versa.
                        operator (inverts
                        boolean value)



           Operator precedence in Java
           Operator precedence in Java defines the rules that determine the order in which
           different operators in an expression are evaluated. When an expression contains
           multiple operators, operator precedence dictates which operations are performed
           first. Table 5.14 shows operator precedence as used in Java programs.


           Table 5.14: Operator precedence in Java

            Category                   Operator                              Associativity
            Primary                    () [] .                               Left-to-right
            Unary                      ++ -- + - ~ ! (type) new              Right-to-left
            Multiplicative             * / %                                 Left-to-right
            Additive                   + -                                   Left-to-right
            Shift                      << >>                                 Left-to-right
            Relational and type testing < <= > >= instance of                Left-to-right
            Equality                   == !=                                 Left-to-right


                                                 340
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   340                                                     23/07/2024   12:34
   344   345   346   347   348   349   350   351   352   353   354