Lecture 10 - Operators
Lecture 10 - Operators
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
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 Meaning
+ Unary plus: not necessary to use since numbers are positive without using it
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
4&5 -00000100
Bitwise OR
4&5 -00000101
Bitwise XOR
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