Operators and Expression

Arithmetic operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:

Operator           Result
+                        Addition
–                        Subtraction (also unary minus)
*                         Multiplication
/                          Division
%                       Modulus
++                      Increment
+=                     Addition assignment
–=                     Subtraction assignment
*=                      Multiplication assignment
/=                      Division assignment
%=                   Modulus assignment
– –                    Decrement

The operands of the arithmetic operators must be of a numeric type. You cannot use them on boolean types, but you can use them on char types, since the char type in Java is, essentially, a subset of int.

Relational Operators

The relational operators determine the relationship that one operand has to the other.Specifically, they determine equality and ordering. The relational operators are shown here:

Operator                   Result
==                             Equal to
!=                               Not equal to
>                               Greater than
<                                Less than
>=                             Greater than or equal to
<=                             Less than or equal to

Since the bitwise operators manipulate the bits within an integer, it is important tounderstand what effects such manipulations may have on a value. Specifically, it is useful to know how Java stores integer values and how it represents negative numbers.So, before continuing, let’s briefly review these two topics.

The outcome of these operations is a boolean value. The relational operators aremost frequently used in the expressions that control the if statement and the variousloop statements.

Boolean Logical Operators

The Boolean logical operators shown here operate only on boolean operands. Allof the binary logical operators combine two boolean values to form a result and boolean value.

Operator                         Result

&                                      Logical AND
|                                        Logical OR
^                                       Logical XOR (exclusive OR)
||                                      Short-circuit OR
&&                                   Short-circuit AND
!                                       Logical unary NOT
&=                                   AND assignment
|=                                    OR assignment
^=                                    XOR assignment
==                                   Equal to
!=                                    Not equal to
?:                                   Ternary if-then-else

The logical Boolean operators, &, |, and ^, operate on boolean values in the sameway that they operate on the bits of an integer. The logical ! operator inverts the Boolean state: !true == false and !false == true. The following table shows the effectof each logical operation

A                  B                       A | B                        A & B                     A ^ B                     !A
False          False                 False                      False                       False                   True
True           False                 True                        False                       True                     False
False         True                   True                        False                       True                     True
True           True                   True                       True                          False                   False

Here is a program that is almost the same as the Bit Logic example shown earlier, but it operates on boolean logical values instead of binary bits:

The Bitwise Operators

Java defines several bitwise operators which can be applied to the integer types, long, int, short, char, and byte. These operators act upon the individual bits of their operands.They are summarized in the following table:

Operator                                     Result
~                                                  Bitwise unary NOT
&                                                  Bitwise AND
|                                                    Bitwise OR
^                                                   Bitwise exclusive OR
>>                                                Shift right
>>>                                             Shift right zero fill
<<                                               Shift left
&=                                               Bitwise AND assignment
|=                                                 Bitwise OR assignmentOperator Result
^=                                                Bitwise exclusive OR assignment
>>=                                             Shift right assignment
t>>>=                                         Shift right zero fill assignment
<<=                                             Shift left assignment

Since the bitwise operators manipulate the bits within an integer, it is important tounderstand what effects such manipulations may have on a value. Specifically, it is useful to know how Java stores integer values and how it represents negative numbers.So, before continuing, let’s briefly review these two topics.

Increment and Decrement

The ++ and the – – are Java’s increment and decrement operators.The increment operator increases its operand by one. The decrement operator decreases its operand by one. For example, this statement

😡 = x + 1;
can be rewritten like this by use of the increment operator
x++;
Similarly, this statement:
x = x – 1;
is equivalent to
x–;

The ? Operator

Java includes a special ternary (three-way) operator that can replace certain types of if-then-else statements. This operator is the ?, and it works in Java much like it does in C, C++, and C#. It can seem somewhat confusing at first, but the ? can be used veryeffectively once mastered. The ? has this general form:

expression1 ? expression2 : expression3

Here, expression1 can be any expression that evaluates to a boolean value. If expression1is true, then expression2 is evaluated; otherwise, expression3 is evaluated. The result of the ? operation is that of the expression evaluated. Both expression2 and expression3 are required to return the same type, which can’t be void.Here is an example of the way that the ? is employed:

ratio = denom == 0 ? 0 : num / denom;

Trending Jobs

Welcome Back!

Login to your account below

Retrieve your password

Please enter your username or email address to reset your password.