ICT 1411
Object Oriented
Programming
Lecture 10
Operators in Java
Statements
• Statements are used to accomplish simplest tasks in Java.
• Statement forms simplest Java operations.
• Can be single line or Span to multiple lines.
• Does not necessarily return a value.
• Statement is terminated with a “;”.
• Each compound or block statements can be used and they are surrounded by
curly braces { }.
Simple Statements Block Statement
int i=5;
if (a>b) {
import java.util.Scanner;
int i=5;
i=i++;
}
Expression
• Simplest form of Statements
• Returns a value when evaluated.
• Can be assigned to a variable or can be tested in Java statements.
• Most expressions are a combination of Operators & Operands.
Ex:
//calculation result is assigned to variable ‘circleArea’
circleArea = 3.14 * radius * radius
if (marks > 50) //expression in test condition
Operators
• An operator is a function that has a special symbolic name and is invoked by using
that symbol with an expression.
• Operators in Java can be classified into 5 types:
• Arithmetic Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Unary Operators
• Bitwise Operators
Operators
Arithmetic Operators
• Arithmetic operators are used to perform arithmetic operations on
variables and data.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
Modulo Operation (Remainder
%
after division)
Operators
Assignment Operators
• Assignment operators are used in Java to assign values to variables.
Operator Example Equivalent to
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
Operators
Relational Operators
• Relational operators are used to check the relationship between two operands.
Operator Description Example
== Is Equal To 3 == 5 returns false
!= Not Equal To 3 != 5 returns true
> Greater Than 3 > 5 returns false
< Less Than 3 < 5 returns true
Greater Than or Equal
>= 3 >= 5 returns false
To
<= Less Than or Equal To 3 <= 5 returns true
Operators
Logical Operators
• Logical operators are used to check whether an expression is true or false.
• They are used in decision making.
Operator Example Meaning
true only if both expression1 and
&& (Logical AND) expression1 && expression2
expression2 are true
true if either expression1 or expression2 is
|| (Logical OR) expression1 || expression2
true
! (Logical NOT) !expression true if expression is false and vice versa
Operators
Unary Operators
• Unary operators are used with only one operand.
Operator Meaning
+ Unary plus: not necessary to use since numbers are positive without using it
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- Decrement operator: decrements value by 1
! Logical complement operator: inverts the value of a boolean
Operators
Bitwise Operators
• Bitwise operators in Java are used to perform operations on individual bits.
Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
• A bit has only one of two values that is 0
or 1, where electrical values of 0 stand
for off and 1 stand for on.
• It is a smallest unit of data on a system. It
is binary digit.
Bitwise AND
Bit value 1 Bit value 2 Output
0 0 0
0 1 0
1 0 0
1 1 1
4 - 00000100
5 - 00000101
4&5 -00000100
Bitwise OR
Bit value 1 Bit value 2 Output
0 0 0
0 1 1
1 0 1
1 1 1
4 - 00000100
5 - 00000101
4&5 -00000101
Bitwise XOR
Bit value 1 Bit value 2 Output
0 0 0
0 1 1
1 0 1
1 1 0
4 - 00000100
5 - 00000101
4&5 -00000001
Left Shift
• Shift the bits of a number to the left by a specified number of positions.
4 - 00000100
4 << 2 - 00010000
Right Shift
• Shift the bits of a number to the right by a specified number of positions.
12 - 00001100
12 >> 2 - 00000011
Operator Precedence
• Determines the order in which expressions are evaluated.
Example
y=6+3*2
The value of y will be 12 because 3*2 will be evaluated first as * has a higher
precedence than +.
. [] ( )
++ -- ! ~
High new
*/ %
+-
<< >> >>> Operators in the same
< > <= >= line have same
== != Precedence and evaluated
& Left to right
^
|
&&
||
Low ?:
= += - = ect.
&= |= <<= etc.
Reserved words / Key words in Java