SlideShare a Scribd company logo
operators and expression
Java
Introduction
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
The 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:
• Assume integer variable A holds 10 and variable B holds 20 then:
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
/ 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 - Increase the value of operand by 1 B++ gives 21
-- Decrement - Decrease the value of operand by 1 B-- gives 19
The Relational Operators
There are following relational operators supported by Java language.
• Assume variable A holds 10 and variable B holds 20 then:
Operator Description Example
== Checks if the value of two operands are equal or not, if yes then condition
becomes true.
(A == B) is not
true.
!= Checks if the value of two operands are equal or not, if values are not
equal then condition becomes true.
(A != B) is true.
> 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.
< 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.
>= 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.
<= 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.
The Bitwise Operators:
• Java defines several bitwise operators which can be applied to
the integer types, long, int, short, char, and byte.
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100
| Binary OR Operator copies a bit if it exists in eather operand. (A | B) will give 61 which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001
~ Binary Ones Complement Operator is unary and has the effect of 'flipping'
bits.
(~A ) will give -61 which is 1100 0011
in 2's complement form due to a
signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the
number of bits specified by the right operand.
A << 2 will give 240 which is 1111
0000
>> Binary Right Shift Operator. The left operands value is moved right by the
number of bits specified by the right operand.
A >> 2 will give 15 which is 1111
>>> Shift right zero fill operator. The left operands value is moved right by the
number of bits specified by the right operand and shifted values are filled up
with zeros.
A >>>2 will give 15 which is 0000 1111
The Logical Operators
• The following table lists the logical operators. Assume boolean
variables A holds true and variable B holds false then:
Operator Description Example
&&
Called Logical AND operator. If both the operands are
non zero then then condition becomes true.
(A && B) is false.
||
Called Logical OR Operator. If any of the two operands
are non zero then then condition becomes true.
(A || B) is true.
!
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.
The Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns values from right side operands
to left side operand
C = A + B will assigne 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
*= 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
<<= 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
Misc Operators
• 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 :
variable x = (expression) ? value if true : value if false
• instanceOf Operator
This operator is used only for object reference variables. The operator
checks whether the object is of a particular type(class type or interface
type). instanceOf operator is written as:
( Object reference variable ) instanceOf (class/interface type)
Precedence of Java Operators
Category Operator Associatively
Postfix () [] . (dot operator) Left to right
Unary ++ - - ! ~ Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift >> >>> << Left to right
Relational > >= < <= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right
itft-Operators in java

More Related Content

PPTX
Operators in java
PPTX
OCA JAVA - 3 Programming with Java Operators
PPTX
Operators in java presentation
PDF
Operators in java
PDF
Java basic operators
PPTX
05 operators
PPT
ODP
Operators
Operators in java
OCA JAVA - 3 Programming with Java Operators
Operators in java presentation
Operators in java
Java basic operators
05 operators
Operators

What's hot (19)

PPT
CBSE Class XI :- Operators in C++
PPTX
Operators in Java
PPTX
Operator.ppt
PPT
PPTX
Operators and expressions
PPTX
Operators in c++
PDF
Operators in c programming
PPT
C Prog. - Operators and Expressions
PPTX
Operator 04 (js)
DOC
Report on c
PDF
Operators in python
PPT
Operators and Expressions in C++
PPTX
Operators in C & C++ Language
PPTX
Operators and Expressions in Java
PDF
Conditional operators
 
ODP
operators in c++
PPTX
Logical and Conditional Operator In C language
PPT
Operator & Expression in c++
PPTX
Operators in Python
CBSE Class XI :- Operators in C++
Operators in Java
Operator.ppt
Operators and expressions
Operators in c++
Operators in c programming
C Prog. - Operators and Expressions
Operator 04 (js)
Report on c
Operators in python
Operators and Expressions in C++
Operators in C & C++ Language
Operators and Expressions in Java
Conditional operators
 
operators in c++
Logical and Conditional Operator In C language
Operator & Expression in c++
Operators in Python
Ad

Viewers also liked (7)

PDF
Operators in java
PDF
Java threading
PDF
15 bitwise operators
PPSX
Data types, Variables, Expressions & Arithmetic Operators in java
PPT
Packages and interfaces
PPTX
Multithreading in java
PPTX
Operators in java
Operators in java
Java threading
15 bitwise operators
Data types, Variables, Expressions & Arithmetic Operators in java
Packages and interfaces
Multithreading in java
Operators in java
Ad

Similar to itft-Operators in java (20)

PDF
Java basic operators
PPTX
PPT ON JAVA AND UNDERSTANDING JAVA'S PRINCIPLES
PPTX
Computer programming 2 Lesson 7
PPTX
Session03 operators
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
PPTX
11operator in c#
PPTX
OOPJ_PPT2,JAVA OPERATORS TPYE WITH EXAMPLES.pptx
PPTX
Java Operators with Simple introduction.pptx
PPTX
L3 operators
PPTX
L3 operators
PPTX
L3 operators
PPTX
Opeartor &amp; expression
PPTX
Python operators part2
PPTX
Arithmetic Operators ____ java.pptx
PPTX
Operators
PPTX
Logical Operators C/C++ language Programming
PPTX
Java Operators with Simple introduction.pptx
PPT
Java - Operators
PPTX
Oop using JAVA
Java basic operators
PPT ON JAVA AND UNDERSTANDING JAVA'S PRINCIPLES
Computer programming 2 Lesson 7
Session03 operators
4_A1208223655_21789_2_2018_04. Operators.ppt
11operator in c#
OOPJ_PPT2,JAVA OPERATORS TPYE WITH EXAMPLES.pptx
Java Operators with Simple introduction.pptx
L3 operators
L3 operators
L3 operators
Opeartor &amp; expression
Python operators part2
Arithmetic Operators ____ java.pptx
Operators
Logical Operators C/C++ language Programming
Java Operators with Simple introduction.pptx
Java - Operators
Oop using JAVA

More from Atul Sehdev (9)

PDF
itft-Overview of java language
PDF
itft-Java evolution
PDF
itft-Inheritance in java
PDF
itft-Fundamentals of object–oriented programming in java
PDF
itft-Decision making and branching in java
PDF
ITFT-Constants, variables and data types in java
PDF
ITFT-Classes and object in java
PDF
ITFT- C,c++,java and world wide web
PDF
ITFT- Applet in java
itft-Overview of java language
itft-Java evolution
itft-Inheritance in java
itft-Fundamentals of object–oriented programming in java
itft-Decision making and branching in java
ITFT-Constants, variables and data types in java
ITFT-Classes and object in java
ITFT- C,c++,java and world wide web
ITFT- Applet in java

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharma ospi slides which help in ospi learning
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Cell Structure & Organelles in detailed.
Pharma ospi slides which help in ospi learning
NOI Hackathon - Summer Edition - GreenThumber.pptx
Anesthesia in Laparoscopic Surgery in India
Open Quiz Monsoon Mind Game Final Set.pptx
Cardiovascular Pharmacology for pharmacy students.pptx
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
UPPER GASTRO INTESTINAL DISORDER.docx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Insiders guide to clinical Medicine.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

itft-Operators in java

  • 2. Introduction 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
  • 3. The 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: • Assume integer variable A holds 10 and variable B holds 20 then: 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 / 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 - Increase the value of operand by 1 B++ gives 21 -- Decrement - Decrease the value of operand by 1 B-- gives 19
  • 4. The Relational Operators There are following relational operators supported by Java language. • Assume variable A holds 10 and variable B holds 20 then: Operator Description Example == Checks if the value of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > 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. < 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. >= 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. <= 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.
  • 5. The Bitwise Operators: • Java defines several bitwise operators which can be applied to the integer types, long, int, short, char, and byte. Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 | Binary OR Operator copies a bit if it exists in eather operand. (A | B) will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 1111 >>> Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. A >>>2 will give 15 which is 0000 1111
  • 6. The Logical Operators • The following table lists the logical operators. Assume boolean variables A holds true and variable B holds false then: Operator Description Example && Called Logical AND operator. If both the operands are non zero then then condition becomes true. (A && B) is false. || Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (A || B) is true. ! 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.
  • 7. The Assignment Operators Operator Description Example = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne 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 *= 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 <<= 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
  • 8. Misc Operators • 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 : variable x = (expression) ? value if true : value if false • instanceOf Operator This operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). instanceOf operator is written as: ( Object reference variable ) instanceOf (class/interface type)
  • 9. Precedence of Java Operators Category Operator Associatively Postfix () [] . (dot operator) Left to right Unary ++ - - ! ~ Right to left Multiplicative * / % Left to right Additive + - Left to right Shift >> >>> << Left to right Relational > >= < <= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left Comma , Left to right