SlideShare a Scribd company logo
1 of 22
Blue Ridge Public School
Std: IX
Subject: Computer Applications
Operators in JAVA
Blue
Ridge
Public
School
1
Operators and Operands
Blue
Ridge
Public
School
2
 Operator is a symbol or token used to perform
arithmetical or logical operations.
 Operands are objects or elements that are acted upon by
operators.
 Based on the number of operands, Operators are
classified as follows:
 Unary Operator
 Binary Operator
 Ternary Operator
Unary Operator
Blue
Ridge
Public
School
3
 Unary operators require only one operand.
 They are used to perform various operations like:
 Negating or reversing the sign of an operand
 Incrementing/decrementing a value by one
 Inverting the value of a boolean
 Unary (+) and (-) Operator:
 Consider the following code snippet
 public class OperatorEg1{
 public static void main(String [] args)
 {
 int a = 8, b = -10;
 System.out.println(+a);
 System.out.println(-a);
 System.out.println(+b);
 System.out.println(-b);
 }
}
 The output of the above code would be:
 8
 -8
 -10
 10
Unary Incrementand DecrementOperators
Blue
Ridge
Public
School
4
 Unary increment operator (++) increases the value of an
operand by one.
 x = x + 1 can be written as ++x or x++
 Unary decrement operator (--) decreases the value of
an operand by one.
 x = x – 1 can be written as ––x or x--
 Increment or Decrement unary operators are of two
types:
 Prefix
 Postfix
Prefix Unary Operator
Blue
Ridge
Public
School
5
 These are applied before the operand.
 The prefix form increments/decrements the value first, and
then performs the specified operation.
 Consider the following code snippet:
 public class prefixExample {
public static void main(String [] args) {
int a = 5;
int b = ++a;
System.out.println(a);
System.out.println(b);
}
}
 The output of the above code would be:
 6
6
Postfix Unary Operator
Blue
Ridge
Public
School
6
 These are applied after the operand.
 The postfix form performs the specified operation and then
increments/decrements the value.
 Consider the following code snippet:
 public class postfixExample {
public static void main (String [] args) {
int a = 3;
int b = a--;
System.out.println(a);
System.out.println(b);
}
}
 The output of the above code would be:
 2
 3
Prefix and Postfix Operator Example
Blue
Ridge
Public
School
7
 Consider the following code snippet:
 public class OperatorExample {
public static void main(String [] args) {
int x = 10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
}
}
 The output of the above code would be:
 10
 12
 12
 10
Logical Complement Unary Operator
Blue
Ridge
Public
School
8
 The logical operator !(NOT) is used when the result of a boolean
expression has to be reversed.
 For e.g. !(8 > 3) will give the result as false because (8 > 3) is
true
 Consider the following code snippet:
 public class OperatorExample2 {
public static void main (String [] args) {
boolean flag = true;
boolean check = false;
System.out.println(!flag);
System.out.println(!check);
}
}
 The output of the above code would be:
 false
 true
Summary of Unary Operators
Blue
Ridge
Public
School
9
Binary Arithmetic Operators
Blue
Ridge
Public
School
10
 An arithmetic operator that operates with two operands is
known as a binary arithmetic operator.
 They are used to perform addition, subtraction, multiplication,
division and find remainder/modulus.
Arithmetic expressions
Blue
Ridge
Public
School
11
 An expression is a combination of one or more operands and
their operators.
 Arithmetic expressions compute numeric results and make use
of the arithmetic operators.
Arithmetic
Expression
Java Expression Comments
x + y
2
(x + y)/2 The parentheses are
required; else it will be
computed as x + y/2
xy
2
x*y/2 Parentheses are not
required as the operators
being of the same
precedence are evaluated
from left to right
a2+b2-c2 a*a + b*b – c*c
1ab + 1cd
3 2
1.0f/3.0f*a*b+1.0f/2.0f*c
*d
1 and 1 will be treated as
3 2
integer division and will
result in a value of 0. Hence
enforce float division by
adding decimal point with a
trailing f/F
Shorthand expressions
Blue
Ridge
Public
School
12
 Java allows the use of shorthand binary operations.
 For e.g.
 a = a + b can be written as a +=b
 m = m * 10 can be written as m *= 10
 c = c – d can be written as c -= d
 d = d/2 can be written as d /= 2
 x = x % 2 can be written as x %= 2
 Shorthand expressions can be written only when the same variable is
to be used both after and before the assignment sign.
 If a = 12, b = 8 find the value of a* = ++a/6 + b++ % 3;
 a = a* (++a/6 + b++ % 3)
 12 * (13/6 + 8%3)
 12* (2 + 2)
 48
 Thus a = 48
Evaluate (Notebook work)
Blue
Ridge
Public
School
13
 If a = 4, b = 3 find the value of c = a++ * 6 + ++b * 5 + b;
 If x = 4, find the value of x+ = x++ * ++x % 2;
 If m = 12, find the value of n = m++ * 5 + --m;
 If y = 14, find the value of z = ++y * (y++ + 5);
 If m = 5 and n = 2, find the value of n after the execution of the
statement n = m + m/n;
Ternary Operator
Blue
Ridge
Public
School
14
 A ternary operator deals with three operands.
 In Java, it is a type of conditional operator.
 The ternary operator (? :) is used to evaluate a boolean
expression.
 The operator decides which value to assign to the variable on
evaluation of the expression.
 Syntax is as follows:
 variable = (condition) ? expression1 : expression2
 The above statement means that if the condition returns true,
expression1 gets assigned to the variable else expression2 gets
stored into the variable.
Relational Operators
Blue
Ridge
Public
School
15
 These are binary operators used to check the relation between
two operands, including equality, greater than, less than etc.
 They return a boolean result after the comparison and are
extensively used in conditional statements.
Logical Operators
Blue
Ridge
Public
School
16
 Logical operators are used to check if a given expression is true or
false.
 They are used in decision making.
 The logical AND (&&) operator is used as follows:
 (a > b) && (a > c)
 It evaluates both the conditions and returns true only when both
the conditions are true, for all other cases it returns false.
 The logical OR (||) operator is used as follows:
 (a==b)||(a==c)
 It returns only false when both the conditions are false, in all
other cases it returns true.
 Precedence of logical operators is NOT(!), AND(&&) and OR(||)
 Evaluate 5 + 3 > 7 && 7 >= 4 + 3
 Note: order of operator precedence is Arithmetic, Relational and then
Logical
Bitwise Logical Operators
Blue
Ridge
Public
School
17
 Bitwise AND (&)
 It is a binary operator that returns 1 if and only if both bits are
1, else returns 0.
 Bitwise Inclusive OR (|)
 It is a binary operator that returns 1 if either of the bit is 1, else
returns 0.
x y x & y
0 0 0
0 1 0
1 0 0
1 1 1
x y x | y
0 0 0
0 1 1
1 0 1
1 1 1
Bitwise Logical Operators
Blue
Ridge
Public
School
18
 Bitwise Exclusive OR (^)
 It is a binary operator that returns 0 if both the bits are same
else returns 1.
x y x ^ y
0 0 0
0 1 1
1 0 1
1 1 0
 Consider the following code snippet:
 public class OperatorBitwise {
public static void main (String [] args) {
int x = 9, y = 3;
System.out.println(x & y); //bitwise AND 1001 & 0011 = 0001 = 1
System.out.println(x | y); //bitwise OR 1001 | 0011 = 1011 = 11
System.out.println(x ^ y); //bitwise XOR 1001 ^ 0011 = 1010 = 10
}
}
Operator precedence in Java
Blue
Ridge
Public
School
19
 In an expression, operator precedence determines the grouping of
operators with operands and determines how an expression will
evaluate.
 When there are two or more operators in an expression, the
operator with the highest priority will be executed first.
Associativity of operators
Blue
Ridge
Public
School
20
 Associativity specifies the order in which operators with the same
precedence are executed, which can be left to right or right to
left.
Category Operator Associativity
Postfix ++ -- Left to right
Prefix + - ++ -- Right to Left
Multiplicative * / % Left to right
Additive + - Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Logical AND , OR && || Left to right
Logical NOT ! Right to left
Bitwise AND OR XOR & | ^ Left to right
Ternary ?: Right to left
Assignment = += -= *= /= %= Right to left
Assignment (Notebook work)
Blue
Ridge
Public
School
21
 Evaluate the following arithmetic expressions:
 int x = 4, find the value of x = x++ * 2 + 3 * --x;
 int k = 5, j = 9, find the value of k += k++ - ++j + k;
 int x = 4, find the value of x += x++ + ++x + x;
 int y = 10, find the value of z = ++y * (y++ + 5);
 int a = 7, find the value of a+= a++ + ++a + --a + a--;
 Arrange the operators in order of higher precedence to lower precedence:
i. &&
ii. %
iii. >=
iv. ++
 Evaluate the value of n, if value of p = 5, q = 19
 int n = (q-p) > (p-q) ? (q-p) : (p-q);
 Write the Java expression for
i. a2 + b2
2(a + b)
ii. Z = x3 + y3 - xy
3
Blue
Ridge
Public
School
22

More Related Content

Similar to Class_IX_Operators.pptx

Similar to Class_IX_Operators.pptx (20)

C++ Expressions Notes
C++ Expressions NotesC++ Expressions Notes
C++ Expressions Notes
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Operators
OperatorsOperators
Operators
 
05 operators
05   operators05   operators
05 operators
 
Operators
OperatorsOperators
Operators
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
C operators
C operatorsC operators
C operators
 
Coper in C
Coper in CCoper in C
Coper in C
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
This slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptxThis slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptx
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
 

Recently uploaded

NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 

Recently uploaded (20)

NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 

Class_IX_Operators.pptx

  • 1. Blue Ridge Public School Std: IX Subject: Computer Applications Operators in JAVA Blue Ridge Public School 1
  • 2. Operators and Operands Blue Ridge Public School 2  Operator is a symbol or token used to perform arithmetical or logical operations.  Operands are objects or elements that are acted upon by operators.  Based on the number of operands, Operators are classified as follows:  Unary Operator  Binary Operator  Ternary Operator
  • 3. Unary Operator Blue Ridge Public School 3  Unary operators require only one operand.  They are used to perform various operations like:  Negating or reversing the sign of an operand  Incrementing/decrementing a value by one  Inverting the value of a boolean  Unary (+) and (-) Operator:  Consider the following code snippet  public class OperatorEg1{  public static void main(String [] args)  {  int a = 8, b = -10;  System.out.println(+a);  System.out.println(-a);  System.out.println(+b);  System.out.println(-b);  } }  The output of the above code would be:  8  -8  -10  10
  • 4. Unary Incrementand DecrementOperators Blue Ridge Public School 4  Unary increment operator (++) increases the value of an operand by one.  x = x + 1 can be written as ++x or x++  Unary decrement operator (--) decreases the value of an operand by one.  x = x – 1 can be written as ––x or x--  Increment or Decrement unary operators are of two types:  Prefix  Postfix
  • 5. Prefix Unary Operator Blue Ridge Public School 5  These are applied before the operand.  The prefix form increments/decrements the value first, and then performs the specified operation.  Consider the following code snippet:  public class prefixExample { public static void main(String [] args) { int a = 5; int b = ++a; System.out.println(a); System.out.println(b); } }  The output of the above code would be:  6 6
  • 6. Postfix Unary Operator Blue Ridge Public School 6  These are applied after the operand.  The postfix form performs the specified operation and then increments/decrements the value.  Consider the following code snippet:  public class postfixExample { public static void main (String [] args) { int a = 3; int b = a--; System.out.println(a); System.out.println(b); } }  The output of the above code would be:  2  3
  • 7. Prefix and Postfix Operator Example Blue Ridge Public School 7  Consider the following code snippet:  public class OperatorExample { public static void main(String [] args) { int x = 10; System.out.println(x++); System.out.println(++x); System.out.println(x--); System.out.println(--x); } }  The output of the above code would be:  10  12  12  10
  • 8. Logical Complement Unary Operator Blue Ridge Public School 8  The logical operator !(NOT) is used when the result of a boolean expression has to be reversed.  For e.g. !(8 > 3) will give the result as false because (8 > 3) is true  Consider the following code snippet:  public class OperatorExample2 { public static void main (String [] args) { boolean flag = true; boolean check = false; System.out.println(!flag); System.out.println(!check); } }  The output of the above code would be:  false  true
  • 9. Summary of Unary Operators Blue Ridge Public School 9
  • 10. Binary Arithmetic Operators Blue Ridge Public School 10  An arithmetic operator that operates with two operands is known as a binary arithmetic operator.  They are used to perform addition, subtraction, multiplication, division and find remainder/modulus.
  • 11. Arithmetic expressions Blue Ridge Public School 11  An expression is a combination of one or more operands and their operators.  Arithmetic expressions compute numeric results and make use of the arithmetic operators. Arithmetic Expression Java Expression Comments x + y 2 (x + y)/2 The parentheses are required; else it will be computed as x + y/2 xy 2 x*y/2 Parentheses are not required as the operators being of the same precedence are evaluated from left to right a2+b2-c2 a*a + b*b – c*c 1ab + 1cd 3 2 1.0f/3.0f*a*b+1.0f/2.0f*c *d 1 and 1 will be treated as 3 2 integer division and will result in a value of 0. Hence enforce float division by adding decimal point with a trailing f/F
  • 12. Shorthand expressions Blue Ridge Public School 12  Java allows the use of shorthand binary operations.  For e.g.  a = a + b can be written as a +=b  m = m * 10 can be written as m *= 10  c = c – d can be written as c -= d  d = d/2 can be written as d /= 2  x = x % 2 can be written as x %= 2  Shorthand expressions can be written only when the same variable is to be used both after and before the assignment sign.  If a = 12, b = 8 find the value of a* = ++a/6 + b++ % 3;  a = a* (++a/6 + b++ % 3)  12 * (13/6 + 8%3)  12* (2 + 2)  48  Thus a = 48
  • 13. Evaluate (Notebook work) Blue Ridge Public School 13  If a = 4, b = 3 find the value of c = a++ * 6 + ++b * 5 + b;  If x = 4, find the value of x+ = x++ * ++x % 2;  If m = 12, find the value of n = m++ * 5 + --m;  If y = 14, find the value of z = ++y * (y++ + 5);  If m = 5 and n = 2, find the value of n after the execution of the statement n = m + m/n;
  • 14. Ternary Operator Blue Ridge Public School 14  A ternary operator deals with three operands.  In Java, it is a type of conditional operator.  The ternary operator (? :) is used to evaluate a boolean expression.  The operator decides which value to assign to the variable on evaluation of the expression.  Syntax is as follows:  variable = (condition) ? expression1 : expression2  The above statement means that if the condition returns true, expression1 gets assigned to the variable else expression2 gets stored into the variable.
  • 15. Relational Operators Blue Ridge Public School 15  These are binary operators used to check the relation between two operands, including equality, greater than, less than etc.  They return a boolean result after the comparison and are extensively used in conditional statements.
  • 16. Logical Operators Blue Ridge Public School 16  Logical operators are used to check if a given expression is true or false.  They are used in decision making.  The logical AND (&&) operator is used as follows:  (a > b) && (a > c)  It evaluates both the conditions and returns true only when both the conditions are true, for all other cases it returns false.  The logical OR (||) operator is used as follows:  (a==b)||(a==c)  It returns only false when both the conditions are false, in all other cases it returns true.  Precedence of logical operators is NOT(!), AND(&&) and OR(||)  Evaluate 5 + 3 > 7 && 7 >= 4 + 3  Note: order of operator precedence is Arithmetic, Relational and then Logical
  • 17. Bitwise Logical Operators Blue Ridge Public School 17  Bitwise AND (&)  It is a binary operator that returns 1 if and only if both bits are 1, else returns 0.  Bitwise Inclusive OR (|)  It is a binary operator that returns 1 if either of the bit is 1, else returns 0. x y x & y 0 0 0 0 1 0 1 0 0 1 1 1 x y x | y 0 0 0 0 1 1 1 0 1 1 1 1
  • 18. Bitwise Logical Operators Blue Ridge Public School 18  Bitwise Exclusive OR (^)  It is a binary operator that returns 0 if both the bits are same else returns 1. x y x ^ y 0 0 0 0 1 1 1 0 1 1 1 0  Consider the following code snippet:  public class OperatorBitwise { public static void main (String [] args) { int x = 9, y = 3; System.out.println(x & y); //bitwise AND 1001 & 0011 = 0001 = 1 System.out.println(x | y); //bitwise OR 1001 | 0011 = 1011 = 11 System.out.println(x ^ y); //bitwise XOR 1001 ^ 0011 = 1010 = 10 } }
  • 19. Operator precedence in Java Blue Ridge Public School 19  In an expression, operator precedence determines the grouping of operators with operands and determines how an expression will evaluate.  When there are two or more operators in an expression, the operator with the highest priority will be executed first.
  • 20. Associativity of operators Blue Ridge Public School 20  Associativity specifies the order in which operators with the same precedence are executed, which can be left to right or right to left. Category Operator Associativity Postfix ++ -- Left to right Prefix + - ++ -- Right to Left Multiplicative * / % Left to right Additive + - Left to right Relational < <= > >= Left to right Equality == != Left to right Logical AND , OR && || Left to right Logical NOT ! Right to left Bitwise AND OR XOR & | ^ Left to right Ternary ?: Right to left Assignment = += -= *= /= %= Right to left
  • 21. Assignment (Notebook work) Blue Ridge Public School 21  Evaluate the following arithmetic expressions:  int x = 4, find the value of x = x++ * 2 + 3 * --x;  int k = 5, j = 9, find the value of k += k++ - ++j + k;  int x = 4, find the value of x += x++ + ++x + x;  int y = 10, find the value of z = ++y * (y++ + 5);  int a = 7, find the value of a+= a++ + ++a + --a + a--;  Arrange the operators in order of higher precedence to lower precedence: i. && ii. % iii. >= iv. ++  Evaluate the value of n, if value of p = 5, q = 19  int n = (q-p) > (p-q) ? (q-p) : (p-q);  Write the Java expression for i. a2 + b2 2(a + b) ii. Z = x3 + y3 - xy 3

Editor's Notes

  1. THIS IS AN AUDIO PPT,CLICK TO PROCEED