Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

OPERATORS

 Unary,Binary and Ternary


Operators( Arithmetic,Relational,Bitwise,Logical)
 Arithmetic Calculations
 Operator Precedence
OPERATORS
 Operators are special symbols that perform specific operations on one, two, or
three operands, and then return a result.
 There are several types of Operators in Java:
 The Simple Assignment Operator
 The Arithmetic Operators
 The Unary Operators
 The Equality and Relational Operators
 The Conditional Operators
 The Type Comparison Operator instanceof
The Simple Assignment Operator

 Most common operators that you'll use is the simple assignment operator "=“,
 it assigns the value on its right to the operand on its left:

 int marks = 56;


 int total = 100;
 float percent = 56.0;
THE ARITHMETIC OPERATORS

Following arithmetic oprators are provided by Java:


+ additive operator (also used for
String concatenation)
- subtraction operator
* multiplication operator
/ division operator
% remainder operator
EXAMPLE
class ArithmeticExample { number = number % 7;
System.out.println(number);
public static void main (String[] args){ }
int number = 1 + 8; }
System.out.println(number);
number = number - 1;
System.out.println(number);

number = number * 2;
System.out.println(number);

number = number / 2;
System.out.println(number);

number = number + 8;
THE UNARY OPERATORS

 The unary operators require only one operand; they perform various operations such as
incrementing/decrementing a value by one, negating an expression, or inverting the value of a
Boolean.
 + Unary plus operator; indicates positive value (numbers are positive without this, however)
 - Unary minus operator; negates an expression
 ++ Increment operator; increments a value by 1
 -- Decrement operator; decrements a value by 1
 ! Logical complement operator;
 inverts the value of a boolean
THE EQUALITY AND RELATIONAL
OPERATORS
Operator
 The equality and relational operators determine if one operand is == Equal to
greater than, less than, equal to, or not equal to another operand. != Not equal to
 Also known as Comparison Operators > Greater
 int number1 = 1; >= Greater than or
equal to
 int number2 = 2;
< Less than
 System.out.println(number1 == number2); <= Less than or
 System.out.println(number1 != number2); equal to
 System.out.println(number1 > number2);
 System.out.println(number1 < number2);
 System.out.println(number1 <= number2);
THE CONDITIONAL OPERATORS

 The && and || operators perform Conditional-AND and Conditional-OR


operations on two boolean expressions. These operators exhibit "short-
circuiting" behavior, which means that the second operand is evaluated only if
needed.
 int number1 = 1;
 int number2 = 2;
 System.out.println((number1 == 1) && (number2 == 2));
 System.out.println((number1 == 1) || (number2 == 1));
TERNARY OPERATOR ‘?:’

 Another conditional operator is ? : , which can be thought of as shorthand for


an if-then-else statement
 It is called ternary because it uses three operands.
 int number1 = 1;
 int number2 = 2;
 String s = number1>1 ? “Greater than one” : “Less than one”;
 System.out.println(s);
OPERATOR PRECEDENCE
 Operators can be combined into complex expressions

result = total + count / max - offset;

 Operators have a well-defined precedence which determines the order in which they
are evaluated

 DMAS(Division Multiplication Addition Subtraction) rule is applied in Java


Operator Precedence

 Multiplication, division, and remainder are evaluated prior to addition, subtraction,


and string concatenation

 Arithmetic operators with the same precedence are evaluated from left to right, but
parentheses can be used to force the evaluation order
OPERATOR PRECEDENCE
 What is the order of evaluation in the following expressions?

a + b + c + d + e a + b * c - d / e
1 2 3 4 3 2 4 1

a / (b + c) - d % e
2 1 4 3

int a=3,b=3,c=3,d=3,e=3;
a / (b * (c + (d - e))) int f = a + b * c - d / e;
System.out.println(f);
4 3 2 1
OUTPUT????
OPERATOR PRECEDENCE
The operators in the following table are listed according to precedence order. The closer to the top of the
table an operator appears, the higher its precedence
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative */%
additive +-
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ?:
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

You might also like