Ch4 - Operators in Java
Ch4 - Operators in Java
Operator – is a symbol or token, which performs arithmetical or logical operations and gives
meaningful result.
Operator
c= a + b
Operands
eg. a + b
eg. c = a + b
Types of Operators
eg. + - * / %
2. Relational Operators – used to compare the value of two operands and determine the
relationship between them. Its result will always be a boolean value (true or false).
3. Logical Operators - used to compare two relational expressions. Result will be true or
false.
These are arithmetical expression in which only java arithmetic operators are used.
ab (1/3) * a * b
2( l+b) 2 * ( l+b)
a*a
eg. + - ++ --
eg. int a = 6;
a++; or ++a;
System.out.println (a);
Output 7
eg. int a = 6;
a--; or --a;
System.out.println (a);
Output 5
Difference between
Prefix Postfix
++a a++
Counter Accumulator
s=0;
c=0;
s=s+a;
c++;
** where a is the input value
Unary Binary
a++ a+b
= ==
a=5 if ( a == b )
/ %
eg. 5 % 2 = 1
eg. 5 / 2 = 2
6%2=0
5.0 / 2 = 2.5
4%6=4
eg. + - * / %
eg. X = ( a < b ) ? 20 : 5
Define :
1. Dot operator – facilitates invoking members of the class to carry out tasks.
2. new operator – used for dynamic allocation of the object in memory space
Output Statement
System.out.print(“of Luck”);
eg. System.out.println(“Best”);
System.out.print(“of Luck”);
Output Best
of Luck