Chapter 5: Operators and Expressions
5.1 Operators
Is a symbol that tells the computer to perform certain mathematical or logical manipulations.
Are used in programs to manipulate data and variables.
Operators can be classified into 8 categories:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
5.1.1 Arithmetic Operators
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
5.1.1.1 Integer Arithmetic
When both the operands in a single arithmetic expression are integers, the expression is
called an Integer Expression and the operation is called Integer Arithmetic.
5.1.1.2 Real Arithmetic
An arithmetic operation involving only real operands is called Real Arithmetic.
A real operand may assume values either in decimal or exponential notation.
5.1.1.3 Mixed – mode Arithmetic
When one of the operands is real and the other is integer, the expression is called a
mixed-mode arithmetic expression.
If either operand is of the real type, then the other operand is converted to real and the
real arithmetic is performed.
5.1.2 Relational Operators
Contains only one relational operator
The general form is:
Ae-1 relational operator ae-2
Ae-1 and ae-2 are arithmetic expressions, which may be simple constants,
variables or combination of them.
When arithmetic expressions are used on either side of a relational operator, the
arithmetic expressions will be evaluated first and then the results compared. That is,
arithmetic operations have a higher priority over relational operations.
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
, != Is not equal to
5.1.3 Logical Operators
The logical operators && and || are used when we want to form compound conditions
by combining two or more relations.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
5.1.4 Assignment Operators
Assignment operators are used to assign the value of an expression to a variable.
General form is:
V op= exp;
V is a variable
Exp is an expression
Op is a java binary operator
Op= is the shorthand assignment operator
Shorthand Assignment Operators
Statement with simple assignment operator Statement with shorthand operator
a=a+1 a += 1
a=a–1 a -= 1
a = a * (n+1) a *= n+1
a = a / (n-1) a /= n+1
a=a%b a %= b
5.1.5 Increment and Decrement Operators
The increment and decrement operators are ++ and –
The operator ++ adds 1 to the operand
The operator - - subtracts 1
5.1.6 Conditional Operator
The character pair ? : is a ternary operator available in Java. This operator is used to
construct conditional expressions of the form:
exp1 ? exp2 : exp3
Where exp1, exp2, and exp3 are expressions.
The operator ? : works as follows:
exp1 is evaluated first. If it is nonzero (true), then the exp2 is evaluated and
becomes the value of the conditional expression.
If exp1 is false, exp3 is evaluated and becomes the value of the conditional
expression.
5.1.7 Bitwise Operators
Operator Meaning
& Bitwise AND
! Bitwise OR
^ Bitwise exclusive OR
~ One’s complement
<< Shift left
5.1 Operators
Is a symbol that tells the computer to perform certain mathematical or logical manipulations.
Are used in programs to manipulate data and variables.
Operators can be classified into 8 categories:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
5.1.1 Arithmetic Operators
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
5.1.1.1 Integer Arithmetic
When both the operands in a single arithmetic expression are integers, the expression is
called an Integer Expression and the operation is called Integer Arithmetic.
5.1.1.2 Real Arithmetic
An arithmetic operation involving only real operands is called Real Arithmetic.
A real operand may assume values either in decimal or exponential notation.
5.1.1.3 Mixed – mode Arithmetic
When one of the operands is real and the other is integer, the expression is called a
mixed-mode arithmetic expression.
If either operand is of the real type, then the other operand is converted to real and the
real arithmetic is performed.
5.1.2 Relational Operators
Contains only one relational operator
The general form is:
Ae-1 relational operator ae-2
Ae-1 and ae-2 are arithmetic expressions, which may be simple constants,
variables or combination of them.
When arithmetic expressions are used on either side of a relational operator, the
arithmetic expressions will be evaluated first and then the results compared. That is,
arithmetic operations have a higher priority over relational operations.
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
, != Is not equal to
5.1.3 Logical Operators
The logical operators && and || are used when we want to form compound conditions
by combining two or more relations.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
5.1.4 Assignment Operators
Assignment operators are used to assign the value of an expression to a variable.
General form is:
V op= exp;
V is a variable
Exp is an expression
Op is a java binary operator
Op= is the shorthand assignment operator
Shorthand Assignment Operators
Statement with simple assignment operator Statement with shorthand operator
a=a+1 a += 1
a=a–1 a -= 1
a = a * (n+1) a *= n+1
a = a / (n-1) a /= n+1
a=a%b a %= b
5.1.5 Increment and Decrement Operators
The increment and decrement operators are ++ and –
The operator ++ adds 1 to the operand
The operator - - subtracts 1
5.1.6 Conditional Operator
The character pair ? : is a ternary operator available in Java. This operator is used to
construct conditional expressions of the form:
exp1 ? exp2 : exp3
Where exp1, exp2, and exp3 are expressions.
The operator ? : works as follows:
exp1 is evaluated first. If it is nonzero (true), then the exp2 is evaluated and
becomes the value of the conditional expression.
If exp1 is false, exp3 is evaluated and becomes the value of the conditional
expression.
5.1.7 Bitwise Operators
Operator Meaning
& Bitwise AND
! Bitwise OR
^ Bitwise exclusive OR
~ One’s complement
<< Shift left