SlideShare a Scribd company logo
Title : Java Programming Language
Muhammad Atif Noori
noorithemes@gmail.com
www.facebook.com/pakcoders
www.facebook.com/pakcoders
1
Java Basic Operators
Java provides a rich set of operators to manipulate variables. We can divide all the Java
operators into the following groups −
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
www.facebook.com/pakcoders
2
Java Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are used
in algebra. The following table lists the arithmetic operators.
www.facebook.com/pakcoders
3
Operator Description Example
+ (Addition)
Adds values on either side of the
operator.
A + B will give 30
- (Subtraction)
Subtracts right-hand operand from
left-hand operand. A - B will give -10
* (Multiplication)
Multiplies values on either side of the
operator. A * B will give 200
Java Arithmetic Operators
www.facebook.com/pakcoders
4
/ (Division)
Divides left-hand operand by
right-hand operand.
B / A will give 2
% (Modulus)
Divides left-hand operand by
right-hand operand and
returns remainder.
B % A will give 0
++ (Increment)
Increases the value of operand
by 1.
B++ gives 21
-- (Decrement)
Decreases the value of
operand by 1.
B-- gives 19
Example
www.facebook.com/pakcoders
5
class Test{
Public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
Part 1 of 3
Example
www.facebook.com/pakcoders
6
System.out.println("b / a = " + (b / a) );
System.out.println("b % a = " + (b % a) );
System.out.println("c % a = " + (c % a) );
System.out.println("a++ = " + (a++) );
System.out.println("b-- = " + (a--) );
// Check the difference in d++ and ++d
System.out.println("d++ = " + (d++) );
System.out.println("++d = " + (++d) );
}
}
Part 2 of 3
Output
www.facebook.com/pakcoders
7
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++ = 10
b-- = 11
d++ = 25
++d = 27
Part 3 of 3
Java Relational Operators
There are following relational operators supported by Java language.
www.facebook.com/pakcoders
8
Operator Description Example
== (equal to)
Checks if the values of two operands are
equal or not, if yes then condition becomes
true.
(A == B) is not true.
!= (not equal to)
Checks if the values of two operands are
equal or not, if values are not equal then
condition becomes true.
(A != B) is true.
> (greater than)
Checks if the value of left operand is greater
than the value of right operand, if yes then
condition becomes true.
(A > B) is not true.
Java Relational Operators
www.facebook.com/pakcoders
9
< (less than)
Checks if the value of left operand
is less than the value of right
operand, if yes then condition
becomes true.
(A < B) is true.
>= (greater than or equal to)
Checks if the value of left operand
is greater than or equal to the
value of right operand, if yes then
condition becomes true.
(A >= B) is not true.
<= (less than or equal to)
Checks if the value of left operand
is less than or equal to the value
of right operand, if yes then
condition becomes true.
(A <= B) is true.
Example
www.facebook.com/pakcoders
10
class Test{
Public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
Part 1 of 3
Example
www.facebook.com/pakcoders
11
System.out.println("b <= a = " + (b <= a) );
}
}
Part 2 of 3
Output
www.facebook.com/pakcoders
12
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false
Part 3 of 3
Java Logical Operators
The following table lists the logical operators
www.facebook.com/pakcoders
13
Operator Description Example
&& (logical and)
Called Logical AND operator. If both the
operands are non-zero, then the condition
becomes true.
(A && B) is false
|| (logical or)
Called Logical OR Operator. If any of the two
operands are non-zero, then the condition
becomes true.
(A || B) is true
! (logical not)
Called Logical NOT Operator. Use to reverses the
logical state of its operand. If a condition is true
then Logical NOT operator will make false.
!(A && B) is true
Java Assignment Operators
Following are the assignment operators supported by Java language
www.facebook.com/pakcoders
14
Operator Description Example
=
Simple assignment operator. Assigns values
from right side operands to left side operand.
C = A + B will assign
value of A + B into C
+=
Add AND assignment operator. It adds right
operand to the left operand and assign the
result to left operand.
C += A is equivalent to
C = C + A
-=
Subtract AND assignment operator. It
subtracts right operand from the left operand
and assign the result to left operand.
C -= A is equivalent to
C = C – A
Java Assignment Operators
www.facebook.com/pakcoders
15
*=
Multiply AND assignment
operator. It multiplies right
operand with the left operand
and assign the result to left
operand.
C *= A is equivalent to C = C *
A
/= Divide AND assignment
operator. It divides left
operand with the right
operand and assign the result
to left operand.
C /= A is equivalent to C = C / A
Java Assignment Operators
www.facebook.com/pakcoders
16
<<=
Left shift AND assignment
operator.
C <<= 2 is same as C = C << 2
>>=
Right shift AND assignment
operator.
C >>= 2 is same as C = C >> 2
&=
Bitwise AND assignment
operator.
C &= 2 is same as C = C & 2
^=
bitwise exclusive OR and
assignment operator.
C ^= 2 is same as C = C ^ 2
|=
bitwise inclusive OR and
assignment operator.
C |= 2 is same as C = C | 2
Java Miscellaneous Operators
There are few other operators supported by Java Language.
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator consists of three
operands and is used to evaluate Boolean expressions. The goal of the operator is to decide,
which value should be assigned to the variable. The operator is written as −
www.facebook.com/pakcoders
17
variable x = (expression) ? value if true : value if false
Example
www.facebook.com/pakcoders
18
class Test{
Public static void main(String[] args) {
int a = 10;
int b = (a == 1) ? 20 : 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20 : 30;
System.out.println( "Value of b is : " + b );
Part 1 of 2
Output
www.facebook.com/pakcoders
19
Value of b is : 30
Value of b is : 20
Part 2 of 2

More Related Content

What's hot

Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
kunal kishore
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
Surbhi Panhalkar
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 
Different Kinds of Exception in DBMS
Different Kinds of Exception in DBMSDifferent Kinds of Exception in DBMS
Different Kinds of Exception in DBMS
Suja Ritha
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
sivasundari6
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
Operators in java
Operators in javaOperators in java
Operators in java
AbhishekMondal42
 
Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBC
Hemant Sharma
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
Atul Sehdev
 
Files in java
Files in javaFiles in java
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
Dharani Kumar Madduri
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Java features
Java featuresJava features
Java features
Prashant Gajendra
 
32 dynamic linking nd overlays
32 dynamic linking nd overlays32 dynamic linking nd overlays
32 dynamic linking nd overlaysmyrajendra
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 

What's hot (20)

Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Different Kinds of Exception in DBMS
Different Kinds of Exception in DBMSDifferent Kinds of Exception in DBMS
Different Kinds of Exception in DBMS
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBC
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
Files in java
Files in javaFiles in java
Files in java
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
OOP java
OOP javaOOP java
OOP java
 
Java threads
Java threadsJava threads
Java threads
 
Java features
Java featuresJava features
Java features
 
32 dynamic linking nd overlays
32 dynamic linking nd overlays32 dynamic linking nd overlays
32 dynamic linking nd overlays
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 

Similar to Operators In Java Part - 8

Computer programming 2 Lesson 7
Computer programming 2  Lesson 7Computer programming 2  Lesson 7
Computer programming 2 Lesson 7
MLG College of Learning, Inc
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
Emmanuel Alimpolos
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
AbhishekMondal42
 
C# Fundamentals - Basics of OOPS - Part 2
C# Fundamentals - Basics of OOPS - Part 2C# Fundamentals - Basics of OOPS - Part 2
C# Fundamentals - Basics of OOPS - Part 2
iFour Technolab Pvt. Ltd.
 
Java unit1 b- Java Operators to Methods
Java  unit1 b- Java Operators to MethodsJava  unit1 b- Java Operators to Methods
Java unit1 b- Java Operators to Methods
SivaSankari36
 
C# fundamentals Part 2
C# fundamentals Part 2C# fundamentals Part 2
Operators in Python
Operators in PythonOperators in Python
Operators in Python
Anusuya123
 
Operators
OperatorsOperators
Operators
VijayaLakshmi506
 
Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
Raselmondalmehedi
 
5_Operators.pdf
5_Operators.pdf5_Operators.pdf
5_Operators.pdf
NiraliArora2
 
Constructor and destructors
Constructor and destructorsConstructor and destructors
Constructor and destructors
divyalakshmi77
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
rinkugupta37
 
11operator in c#
11operator in c#11operator in c#
11operator in c#
Sireesh K
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
Atul Sehdev
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
michaelaaron25322
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
Shobi P P
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
REHAN IJAZ
 

Similar to Operators In Java Part - 8 (20)

Computer programming 2 Lesson 7
Computer programming 2  Lesson 7Computer programming 2  Lesson 7
Computer programming 2 Lesson 7
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
 
C# Fundamentals - Basics of OOPS - Part 2
C# Fundamentals - Basics of OOPS - Part 2C# Fundamentals - Basics of OOPS - Part 2
C# Fundamentals - Basics of OOPS - Part 2
 
Java unit1 b- Java Operators to Methods
Java  unit1 b- Java Operators to MethodsJava  unit1 b- Java Operators to Methods
Java unit1 b- Java Operators to Methods
 
C# fundamentals Part 2
C# fundamentals Part 2C# fundamentals Part 2
C# fundamentals Part 2
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Operators
OperatorsOperators
Operators
 
Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
 
5_Operators.pdf
5_Operators.pdf5_Operators.pdf
5_Operators.pdf
 
Constructor and destructors
Constructor and destructorsConstructor and destructors
Constructor and destructors
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
 
11operator in c#
11operator in c#11operator in c#
11operator in c#
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
Session03 operators
Session03 operatorsSession03 operators
Session03 operators
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 

Recently uploaded

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 

Recently uploaded (20)

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 

Operators In Java Part - 8

  • 1. Title : Java Programming Language Muhammad Atif Noori noorithemes@gmail.com www.facebook.com/pakcoders www.facebook.com/pakcoders 1
  • 2. Java Basic Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups − Arithmetic Operators Relational Operators Bitwise Operators Logical Operators Assignment Operators Misc Operators www.facebook.com/pakcoders 2
  • 3. Java Arithmetic Operators Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators. www.facebook.com/pakcoders 3 Operator Description Example + (Addition) Adds values on either side of the operator. A + B will give 30 - (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -10 * (Multiplication) Multiplies values on either side of the operator. A * B will give 200
  • 4. Java Arithmetic Operators www.facebook.com/pakcoders 4 / (Division) Divides left-hand operand by right-hand operand. B / A will give 2 % (Modulus) Divides left-hand operand by right-hand operand and returns remainder. B % A will give 0 ++ (Increment) Increases the value of operand by 1. B++ gives 21 -- (Decrement) Decreases the value of operand by 1. B-- gives 19
  • 5. Example www.facebook.com/pakcoders 5 class Test{ Public static void main(String[] args) { int a = 10; int b = 20; int c = 25; int d = 25; System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); Part 1 of 3
  • 6. Example www.facebook.com/pakcoders 6 System.out.println("b / a = " + (b / a) ); System.out.println("b % a = " + (b % a) ); System.out.println("c % a = " + (c % a) ); System.out.println("a++ = " + (a++) ); System.out.println("b-- = " + (a--) ); // Check the difference in d++ and ++d System.out.println("d++ = " + (d++) ); System.out.println("++d = " + (++d) ); } } Part 2 of 3
  • 7. Output www.facebook.com/pakcoders 7 a + b = 30 a - b = -10 a * b = 200 b / a = 2 b % a = 0 c % a = 5 a++ = 10 b-- = 11 d++ = 25 ++d = 27 Part 3 of 3
  • 8. Java Relational Operators There are following relational operators supported by Java language. www.facebook.com/pakcoders 8 Operator Description Example == (equal to) Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != (not equal to) Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > (greater than) Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
  • 9. Java Relational Operators www.facebook.com/pakcoders 9 < (less than) Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= (greater than or equal to) Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= (less than or equal to) Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.
  • 10. Example www.facebook.com/pakcoders 10 class Test{ Public static void main(String[] args) { int a = 10; int b = 20; System.out.println("a == b = " + (a == b) ); System.out.println("a != b = " + (a != b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a < b) ); System.out.println("b >= a = " + (b >= a) ); Part 1 of 3
  • 12. Output www.facebook.com/pakcoders 12 a == b = false a != b = true a > b = false a < b = true b >= a = true b <= a = false Part 3 of 3
  • 13. Java Logical Operators The following table lists the logical operators www.facebook.com/pakcoders 13 Operator Description Example && (logical and) Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false || (logical or) Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. (A || B) is true ! (logical not) Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true
  • 14. Java Assignment Operators Following are the assignment operators supported by Java language www.facebook.com/pakcoders 14 Operator Description Example = Simple assignment operator. Assigns values from right side operands to left side operand. C = A + B will assign value of A + B into C += Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A -= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C – A
  • 15. Java Assignment Operators www.facebook.com/pakcoders 15 *= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. C *= A is equivalent to C = C * A /= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand. C /= A is equivalent to C = C / A
  • 16. Java Assignment Operators www.facebook.com/pakcoders 16 <<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator. C &= 2 is same as C = C & 2 ^= bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2 |= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
  • 17. Java Miscellaneous Operators There are few other operators supported by Java Language. Conditional Operator ( ? : ) Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as − www.facebook.com/pakcoders 17 variable x = (expression) ? value if true : value if false
  • 18. Example www.facebook.com/pakcoders 18 class Test{ Public static void main(String[] args) { int a = 10; int b = (a == 1) ? 20 : 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20 : 30; System.out.println( "Value of b is : " + b ); Part 1 of 2
  • 19. Output www.facebook.com/pakcoders 19 Value of b is : 30 Value of b is : 20 Part 2 of 2