Chapter - 5 Class 9

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

Chapter -5

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.

• These operators consist of various unary and binary


operators that can be applied on a single or two
operands.
Arithmetic
operators

Unary Binary Ternary


operator operators operators
Unary
operator

Unary Unary
Unary + Unary -
increment decrement

Pre Post Pre Post


increment increment decrement decrement
Unary Operators
Java unary operators are the types that need only one
operand to perform any operation like increment,
decrement etc.

• Unary plus: not necessary to use since numbers are


positive without using it
• Unary minus: inverts the sign of an expression
• Increment operator: increments value by 1
• Decrement operator: decrements value by 1
Increment(++)
• Post-increment operator
When placed after the variable name, the value of
the operand is incremented but the previous value is
retained temporarily until the execution of this
statement and it gets updated before the execution
of the next statement.
Example: num = 5
num++ = 6
Pre-increment operator
• When placed before the variable name, the
operand’s value is incremented instantly.
Example: num = 5
++num = 6
Decrement(– –)
• Post-decrement operator
When placed after the variable name, the value of the
operand is decremented but the previous values is
retained temporarily until the execution of this
statement and it gets updated before the execution of
the next statement.
Example: num = 5
num-- = 5
// Value will be decremented before execution of next
statement.
Pre-decrement operator
• When placed before the variable name, the
operand’s value is decremented instantly.
Example: num = 5
--num = 5
//output is 5, value is decremented before
execution of next statement
Binary operator
A binary operator is an operator that operates
on two operands and manipulates them to
return a result.
Ternary operator
• Java ternary operator is the only conditional operator

that takes three operands.

• It’s a one-liner replacement for the if-then-else

statement and is used a lot in Java programming.

• We can use the ternary operator in place of if-else

conditions or even switch conditions using nested

ternary operators. .
Example:
num1 = 10;
num2 = 20;

res=(num1>num2) ? (num1+num2):(num1-num2)

Since num1<num2,
the second operation is performed

res = num1-num2 = -10


Relational Operators

• Relational Operators are a bunch of binary


operators used to check for relations between two
operands, including equality, greater than, less
than, etc.

• They return a Boolean result after the comparison


and are extensively used in looping statements as
well as conditional if-else statements and so on.
Logical operator
Logical operators are used to perform logical “AND”, “OR” and
“NOT” operations, i.e. the function similar to AND gate and
OR gate in digital electronics. They are used to combine two
or more conditions.

• 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,

Syntax: condition1 && condition2


Example:

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.

Syntax: condition1 || condition


Example:
a = 10, b = 20, c = 20
condition1: a < b
condition2: b > c
if(condition1 || condition2)
d = a + b + c // Since one of the condition is true
d = 50.
Logical ‘NOT’ Operator (!)
• This is a unary operator and returns true when the
condition under consideration is not satisfied or is a
false condition. Basically, if the condition is false, the
operation returns true and when the condition is
true, the operation returns false.
Syntax: !(condition)
Example: a = 10, b = 20
!(a<b) // returns false
!(a>b) // returns true
Assignment Operators
These operators are used to assign values to a variable.

• (=) 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.

• It is also used to access classes and sub-packages from a


package.

• It is also used to access the member of a package or a


class.
Type Conversion
In Java, type casting is a method or process that
converts a data type into another data type in both
ways manually and automatically. The automatic
conversion is done by the compiler and manual
conversion performed by the programmer.
Types of Type Casting
There are two types of type casting:
• Widening Type Casting(implicit)
• Narrowing Type Casting(explicit)
Widening Type Casting
• Converting a lower data type into a higher one is called
widening type casting.
• It is also known as implicit conversion or casting down.
• It is done automatically.
• It is safe because there is no chance to lose data. It takes place
when:
Both data types must be compatible with each other.
• The target type must be larger than the source type.

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.

You might also like