Chapter - 5 Class 9
Chapter - 5 Class 9
Chapter - 5 Class 9
Operators
and
Expressions
Operators in java
Operators are symbols that perform
operations on variables and values. For
example, + is an operator used for addition,
while * is also an operator used for
multiplication.
Types of operators
• Arithmetic Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
Arithmetic Operators
• These operators involve the mathematical operators
that can be used to perform various simple or
advanced arithmetic operations on the primitive data
types referred to as the operands.
Unary Unary
Unary + Unary -
increment decrement
ternary operators. .
Example:
num1 = 10;
num2 = 20;
res=(num1>num2) ? (num1+num2):(num1-num2)
Since num1<num2,
the second operation is performed
• AND Operator ( && ) – if( a && b ) [if true execute else don’t]
• OR Operator ( || ) – if( a || b) [if one of them is true to
execute else don’t]
• NOT Operator ( ! ) – !(a<b) [returns false if a is smaller than b]
Logical ‘AND’ Operator (&&)
• This operator returns true when both the conditions
under consideration are satisfied or are true. If even
one of the two yields false, the operator results false.
In Simple terms,
a = 10, b = 20, c = 20
condition1: a < b
condition2: b == c
if(condition1 && condition2)
d = a + b + c // Since both the conditions are
true
d = 50.
Logical ‘OR’ Operator (||)
• This operator returns true when one of the two
conditions under consideration is satisfied or is true.
If even one of the two yields true, the operator results
true. To make the result false, both the constraints
need to return false.
• (=) operator:
This is the most straight forward assignment operator, which
is used to assign the value on the right to the variable on the
left. This is the basic definition of an assignment operator and
how it functions.
Example: a = 10; ch = 'y';
Shorthand Operator in Java
• Some unique Compound Assignment Operators
commonly referred to as Shorthand Assignment
Operators are provided by Java. Because it offers a quick
way to appoint an expression to a variable, it is known
as shorthand.
Example: a = a-7;
a -= 7;
• Java uses a variety of compound assignment operators,
including:
The new operator
• The new operator is used in Java to create new objects. It can
also be used to create an array object.
Let us first see the steps when creating an object from a class
• Declaration − A variable declaration with a variable name
with an object type.
• Instantiation − The 'new' keyword is used to create the
object.
• Initialization − The 'new' keyword is followed by a call to a
constructor. This call initializes the new object.
• Scanner sc = new Scanner(System.in);
• BufferedReader br = BufferedReader(new
InputStreamReader(System.in));
The .(dot) operator
• It denotes the separation of class from a package,
separation of method from the class, and separation of a
variable from a reference variable. It is also known as
separator or period or member operator.
byte -> short -> char -> int -> long -> float -> double
Narrowing Type Casting
• Converting a higher data type into a lower one is called
narrowing type casting.
• It is also known as explicit conversion or casting up.
• It is done manually by the programmer.
• If we do not perform casting then the compiler reports a
compile-time error
double -> float -> long -> int -> char -> short -> byte
What is operator precedence?
• The operator precedence represents how two
expressions are bind together. In an
expression, it determines the grouping of
operators with operands and decides how an
expression will evaluate.
• While solving an expression two things must
be kept in mind the first is a precedence and
the second is associativity.
Precedence
• Precedence is the priority for grouping different
types of operators with their operands.
• It is meaningful only if an expression has more than
one operator with higher or lower precedence.
• The operators having higher precedence are
evaluated first.
• If we want to evaluate lower precedence operators
first, we must group operands by using parentheses
and then evaluate.
Associatively
• We must follow associatively if an expression
has more than two operators of the same
precedence.
• In such a case, an expression can be solved
either left-to-right or right-to-left, accordingly.