SlideShare a Scribd company logo
1 of 19
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

Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Searching in Arrays
Searching in ArraysSearching in Arrays
Searching in ArraysDhiviya Rose
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentationkunal kishore
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVASAGARDAVE29
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | EdurekaEdureka!
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and OperatorsMarwa Ali Eissa
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaAtul Sehdev
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - RecursionAdan Hubahib
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 

What's hot (20)

Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Searching in Arrays
Searching in ArraysSearching in Arrays
Searching in Arrays
 
Java input
Java inputJava input
Java input
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java – lexical issues
Java – lexical issuesJava – lexical issues
Java – lexical issues
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
java token
java tokenjava token
java token
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 

Similar to Operators In Java Part - 8

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
 
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
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++
 

Recently uploaded

History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 

Recently uploaded (20)

History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 

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