Khana-e Noor University
Computer Science Faculty
Fundamentals Of
Programming (Java)
Chapter 3
Outline
Operators
Arithmetic Operators
Relational Operators
Assignment Operators
Operators Precedence in Java
Operators
An operator is a symbol that tells the
compiler to perform specific
mathematical or logical manipulations.
Some of the Operator types in Java:
Arithmetic Operators
Relational Operators
Assignment Operators
Operator Example
int num1 = 22;
int num2 = 5;
int addNums = num1 + num2; // 27
int subtractNums = num1 – num2; // 17
int multiplyNums = num1 * num2; //
110
int divideNums = num1 / num2; // 4
int moduloNums = num1 % num2; // 2
Arithmetic Operators
There are following arithmetic operators
supported by Java language: Assume
variable A holds 10 and variable B holds
20, then:
Arithmetic Operators example
Relational Operators
Assignment Operators
Simple Java Program
Output
Operators to Increment (++) and
Decrement (--)
Sometimes you need to count in increments of one.
This is particularly required in variables that control loops where the
value of the variable needs to be incremented or decremented
every time a loop has been executed.
int num1 = 101;
int num2 = num1++; // Postfix increment operator
int num2 = ++num1; // Prefix increment operator
int num2 = num1--; // Postfix decrement operator
int num2 = --num1; // Prefix decrement operator
Operators Precedence in Java
You possibly learned something in school on
the order of arithmetic operations called
BODMAS (Brackets Orders Division
Multiplication Addition Subtraction),
indicating the order in which a complex
arithmetical expression should be evaluated.
Operators Precedence in Java
In Java, you use operators and expressions
such as the following:
int myNumber = 10 + 30 /15 *5;
Operators Precedence in Java
(),
[],
{}
Power
* (Multiplication), \ (Division)
+,-
Left to Right
Any Questions?