SlideShare a Scribd company logo
Java Operators
 Java provides a rich set of operators to manipulate
variables. We can divide all the Java operators into the
following groups:


• Assignment Operator

•Arithmetic Operators

•Unary Operators

• Equality and Relational Operators

•Conditional (Logical) Operators

•Bitwise and Bit Shift Operators
(1) Assignment Operator


     Simple Assignment Operator
      Syntax of using the assignment operator is:
      <variable> = <expression>;



     Compound Assignment Operator
      Syntax of using the compound assignment operator is:
      operand operation= operand
Compound assignment operators :

Operator Example    Equivalent Expression
+=       x += y;      x = (x + y);

-=      x -= y;       x = (x - y);

*=      x *= y;       x = (x * y);

/=      x /= y;       x = (x / y);

%=      x %= y;       x = (x % y);

&=      x &= y;       x = (x & y);

|=      x != y;       x = (x ! y);

^=      x ^= y;                 x = (x ^ y);

<<=     x <<= y;      x = (x << y);

>>=     x >>= y;      x = (x >> y);

>>>=    x >>>= y;     x = (x >>> y);
(2) Arithmetic Operators
 The symbols of arithmetic operators are given in a table:

Symbol Name of the Operator              Example

+         Additive Operator               n = n + 1;

-         Subtraction Operator            n = n - 1;

*         Multiplication Operator         n = n * 1;

/         Division Operator               n = n / 1;

%         Remainder Operator              n = n % 1;


 The "+" operator can also be used to concatenate (to join) the two strings
together.

 For example:
String str1 = "Concatenation of the first";
String str2 = "and second String";
String result = str1 + str2;
(3) Unary Operators
There are different types of unary operators :


 + Unary plus operator indicates positive value (however, numbers
are              positive without this)
Ex : int number = +1;
   - Unary minus operator negates an expression
Ex : number = - number;
 ++ Increment operator increments a value by 1
Ex : number = ++ number;
 -- Decrement operator decrements a value by 1
Ex : number = -- number;
 ! Logical compliment operator inverts a boolean value
(4) Equality and Relational Operators


Symbol Name of the Operator Example
==     Equal to                   a==b


!=     Not equal to               a!=b
>      Greater than               a>b


<      Less than                  a<b


>=     Greater than or equal to   a>=b


<=     Less than or equal to      a>=b
(5) Conditional   (Logical) Operators


Symbol   Name of the Operator

&        AND

&&       Conditional-AND

|        OR

||       Conditional-OR

!        NOT

?:       Ternary (shorthand for if-then-else statement)
ternary ("?:") operator

Java supports another conditional operator that is known as the ternary operator
"?:" and basically is used for an if-then-else as shorthand as

boolean expression ? operand1 : operand2;




  If we analyze this diagram then we find that, operand1 is returned, if
  the expression is true; otherwise operand2 is returned in case of false
  expression.
Lets have an example implementing some Logical operators:

class ConditionalOperator
{

 public static void main(String[] args)
{
  int x = 5;
  int y = 10, result=0;
  boolean bl = true;
  if((x == 5) && (x < y))
  System.out.println("value of x is "+x);
  if((x == y) || (y > 1))
  System.out.println("value of y is greater than the value of x");
 result = bl ? x : y;
  System.out.println("The returned value is "+result);
 }
}
Output

value of x is 5 is 5
value of y is greater than the value of x lue of y is greater than the value of x
The returned value is 5
(6) Bitwise and Bit Shift Operators
. There are different types of bitwise and bit shift operators available in
the Java language summarized in the table.


Symbol Name of the Operator              Example

~         Unary bitwise complement       ~op2

&         Bitwise AND                    op1 & op2

|         Bitwise inclusive OR           op1 | op2

^         Bitwise exclusive OR           op1 ^ op2

<<        Signed left shift              op1 << op2

>>        Signed right sift              op1 >> op2

>>>       Unsigned right shift           op1 >>> op2
I. Unary Bitwise Complement ("~") :
Lets use the table to understand bitwise complement
operation :

Operand Result

 0         1
 1         0
 1         0
 0         1

II. Bitwise    AND (&) :

Lets understand the AND operations using truth table:

(AND)
 A B      Result
 0 0      0
 1 0      0
 0 1      0
 1 1      1
III. Bitwise inclusive OR ( | ) :
Lets understand the inclusive OR operations using truth
table:
   (OR)
 A        B          Result
 0        0          0
 1        0          1
 0        1          1
 1        1          1


IV. Bitwise exclusive OR (^) :
Lets understand the exclusive OR operations using truth
table:
 A        B         Result
 0        0         0
 1        0         1
 0        1         1
 1        1         0
 Bit Shifts Operators:
I.   Signed Left Shift ("<<") :




This diagram shows that, all bits of the upper position were shifted to the left by
the distance of 1; and the Zero was shifted to the right most position. Thus the
result is returned as 11100.

Another expression "2<<2"; shifts all bits of the number 2 to the left placing a
zero to the right for each blank place. Thus the value 0010 becomes 1000 or 8 in
decimal.
II. Signed Right Shift (">>") :




This diagram shows that, all bits of the upper position were shifted to the right
distance specified by 1; Since the sign bit of this number indicates it as a positive
number so the 0 is shifted to the right most position. Thus the result is returned as
00011 or 3 in decimal.

Another expression "2>>2"; shifts all bits of the number 2 to the right placing a
zero to the left for each blank place. Thus the value 0010 becomes 0000 or 0 in
decimal.
III. Unsigned Right Shift (">>>") :


For example, the expression "14>>>2"; shifts all bits of the
number 14 to the right placing a zero to the left for each blank
place Thus the value 1110 becomes 0011 or 3 in decimal.
Operator Precedence

Operators                     Precedence

array index & parentheses     [] ( )
access object                 .
postfix                       expr++ expr--
unary                         ++expr --expr +expr -expr ~ !
multiplicative                * / %
additive                      + -
bit shift                     << >> >>>
relational                    < > <= >=
equality                      == !=
bitwise AND                   &
bitwise exclusive OR          ^
bitwise inclusive OR          |
logical AND                   &&
logical OR                    ||
ternary                       ?:
assignment                   = += -= *= /= %= &= ^= |=        <<= >>= >> >=
 Lets see an example that evaluates an arithmetic
expression according to the precedence order.


class PrecedenceDemo
{
    public static void main(String[] args)
{
    int a = 6;
    int b = 5;
    int c = 10;
    float rs = 0;
    rs = a + (++b)* ((c / a)* b);
    System.out.println("The result is:" + rs);
    }
}
 The expression "a+(++b)*((c/a)*b)" is evaluated      from right
to left. Its evaluation order depends upon the precedence order of
the operators. It is shown below:

(++b)                a + (++b)*((c/a)*b)
(c/a)                a+ (++b)*((c/a)*b)
(c/a)*b              a + (++b)*((c/a)* b)
(++b)*((c/a)*b)      a + (++b)*((c/a)* b)
a+(++b)*((c/a)*b)    a+(++b)*((c/a)*b)


Output

The result is:42.0
Java 2

More Related Content

What's hot (20)

ODP
Operators
jayesh30sikchi
 
ODP
operators in c++
Kartik Fulara
 
PPTX
Operators in c++
ABHIJITPATRA23
 
PPT
C Prog. - Operators and Expressions
vinay arora
 
PPTX
Operators and expressions
vishaljot_kaur
 
PPTX
Operator.ppt
Darshan Patel
 
DOC
Report on c
jasmeen kr
 
PPT
Operators
Devi Pradeep Podugu
 
PPT
Operators and Expressions in C++
Praveen M Jigajinni
 
PPTX
Operators and Expressions in Java
Abhilash Nair
 
PPT
Arithmetic operator
Jordan Delacruz
 
ODP
operators in c++
Kartik Fulara
 
PDF
Operators in java
Ravi_Kant_Sahu
 
PPT
Operator & Expression in c++
bajiajugal
 
PDF
Chapter 5 - Operators in C++
Deepak Singh
 
PDF
Operators in c programming
savitamhaske
 
PPTX
Operators in Java
Rhythm Suiwal
 
PPTX
Operator 04 (js)
AbhishekMondal42
 
PPTX
Operators
Then Murugeshwari
 
PPT
Operation and expression in c++
Online
 
Operators
jayesh30sikchi
 
operators in c++
Kartik Fulara
 
Operators in c++
ABHIJITPATRA23
 
C Prog. - Operators and Expressions
vinay arora
 
Operators and expressions
vishaljot_kaur
 
Operator.ppt
Darshan Patel
 
Report on c
jasmeen kr
 
Operators and Expressions in C++
Praveen M Jigajinni
 
Operators and Expressions in Java
Abhilash Nair
 
Arithmetic operator
Jordan Delacruz
 
operators in c++
Kartik Fulara
 
Operators in java
Ravi_Kant_Sahu
 
Operator & Expression in c++
bajiajugal
 
Chapter 5 - Operators in C++
Deepak Singh
 
Operators in c programming
savitamhaske
 
Operators in Java
Rhythm Suiwal
 
Operator 04 (js)
AbhishekMondal42
 
Operation and expression in c++
Online
 

Similar to Java 2 (20)

PPTX
Operators in Python
Anusuya123
 
PPTX
Arithmetic Operators ____ java.pptx
gnyanadeepa
 
PPTX
Operators expressions-and-statements
CtOlaf
 
PPTX
btwggggggggggggggggggggggggggggggisop correct (1).pptx
Orin18
 
PDF
Types of Operators in C
Thesis Scientist Private Limited
 
PPT
C Sharp Jn (2)
guest58c84c
 
PPT
C Sharp Jn (2)
jahanullah
 
PPTX
C++ revision add on till now
AmAn Singh
 
PPTX
C++ revision add on till now
AmAn Singh
 
PPTX
Operators and expressions in C++
Neeru Mittal
 
PPTX
Operators
VijayaLakshmi506
 
PDF
SPL 6 | Operators in C
Mohammad Imam Hossain
 
PPTX
Operators inc c language
Tanmay Modi
 
PPTX
Operators and expressions in c language
tanmaymodi4
 
DOCX
C – operators and expressions
Chukka Nikhil Chakravarthy
 
PPT
Operators
Kamran
 
PDF
C++ Expressions Notes
Prof Ansari
 
PPT
6 operators-in-c
Rohit Shrivastava
 
PPT
6 operators-in-c
Rohit Shrivastava
 
PDF
Java unit 3
Shipra Swati
 
Operators in Python
Anusuya123
 
Arithmetic Operators ____ java.pptx
gnyanadeepa
 
Operators expressions-and-statements
CtOlaf
 
btwggggggggggggggggggggggggggggggisop correct (1).pptx
Orin18
 
Types of Operators in C
Thesis Scientist Private Limited
 
C Sharp Jn (2)
guest58c84c
 
C Sharp Jn (2)
jahanullah
 
C++ revision add on till now
AmAn Singh
 
C++ revision add on till now
AmAn Singh
 
Operators and expressions in C++
Neeru Mittal
 
Operators
VijayaLakshmi506
 
SPL 6 | Operators in C
Mohammad Imam Hossain
 
Operators inc c language
Tanmay Modi
 
Operators and expressions in c language
tanmaymodi4
 
C – operators and expressions
Chukka Nikhil Chakravarthy
 
Operators
Kamran
 
C++ Expressions Notes
Prof Ansari
 
6 operators-in-c
Rohit Shrivastava
 
6 operators-in-c
Rohit Shrivastava
 
Java unit 3
Shipra Swati
 
Ad

Recently uploaded (20)

PPTX
Room booking management - Meeting Room In Odoo 17
Celine George
 
PPTX
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
PDF
Right to Information.pdf by Sapna Maurya XI D
Directorate of Education Delhi
 
PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PPTX
national medicinal plants board mpharm.pptx
SHAHEEN SHABBIR
 
PPTX
Various Psychological tests: challenges and contemporary trends in psychologi...
santoshmohalik1
 
PPTX
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
PPTX
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
THE HUMAN INTEGUMENTARY SYSTEM#MLT#BCRAPC.pptx
Subham Panja
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
PDF
07.15.2025 - Managing Your Members Using a Membership Portal.pdf
TechSoup
 
PPTX
HIRSCHSPRUNG'S DISEASE(MEGACOLON): NURSING MANAGMENT.pptx
PRADEEP ABOTHU
 
PPTX
Mrs Mhondiwa Introduction to Algebra class
sabinaschimanga
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
A guide to responding to Section C essay tasks for the VCE English Language E...
jpinnuck
 
PPTX
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
PDF
Stepwise procedure (Manually Submitted & Un Attended) Medical Devices Cases
MUHAMMAD SOHAIL
 
PPTX
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
Room booking management - Meeting Room In Odoo 17
Celine George
 
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
Right to Information.pdf by Sapna Maurya XI D
Directorate of Education Delhi
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
national medicinal plants board mpharm.pptx
SHAHEEN SHABBIR
 
Various Psychological tests: challenges and contemporary trends in psychologi...
santoshmohalik1
 
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
THE HUMAN INTEGUMENTARY SYSTEM#MLT#BCRAPC.pptx
Subham Panja
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
07.15.2025 - Managing Your Members Using a Membership Portal.pdf
TechSoup
 
HIRSCHSPRUNG'S DISEASE(MEGACOLON): NURSING MANAGMENT.pptx
PRADEEP ABOTHU
 
Mrs Mhondiwa Introduction to Algebra class
sabinaschimanga
 
PPT on the Development of Education in the Victorian England
Beena E S
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
A guide to responding to Section C essay tasks for the VCE English Language E...
jpinnuck
 
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
Stepwise procedure (Manually Submitted & Un Attended) Medical Devices Cases
MUHAMMAD SOHAIL
 
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
Ad

Java 2

  • 2.  Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: • Assignment Operator •Arithmetic Operators •Unary Operators • Equality and Relational Operators •Conditional (Logical) Operators •Bitwise and Bit Shift Operators
  • 3. (1) Assignment Operator  Simple Assignment Operator Syntax of using the assignment operator is: <variable> = <expression>;  Compound Assignment Operator Syntax of using the compound assignment operator is: operand operation= operand
  • 4. Compound assignment operators : Operator Example Equivalent Expression += x += y; x = (x + y); -= x -= y; x = (x - y); *= x *= y; x = (x * y); /= x /= y; x = (x / y); %= x %= y; x = (x % y); &= x &= y; x = (x & y); |= x != y; x = (x ! y); ^= x ^= y; x = (x ^ y); <<= x <<= y; x = (x << y); >>= x >>= y; x = (x >> y); >>>= x >>>= y; x = (x >>> y);
  • 5. (2) Arithmetic Operators  The symbols of arithmetic operators are given in a table: Symbol Name of the Operator Example + Additive Operator n = n + 1; - Subtraction Operator n = n - 1; * Multiplication Operator n = n * 1; / Division Operator n = n / 1; % Remainder Operator n = n % 1;  The "+" operator can also be used to concatenate (to join) the two strings together.  For example: String str1 = "Concatenation of the first"; String str2 = "and second String"; String result = str1 + str2;
  • 6. (3) Unary Operators There are different types of unary operators :  + Unary plus operator indicates positive value (however, numbers are positive without this) Ex : int number = +1;  - Unary minus operator negates an expression Ex : number = - number;  ++ Increment operator increments a value by 1 Ex : number = ++ number;  -- Decrement operator decrements a value by 1 Ex : number = -- number;  ! Logical compliment operator inverts a boolean value
  • 7. (4) Equality and Relational Operators Symbol Name of the Operator Example == Equal to a==b != Not equal to a!=b > Greater than a>b < Less than a<b >= Greater than or equal to a>=b <= Less than or equal to a>=b
  • 8. (5) Conditional (Logical) Operators Symbol Name of the Operator & AND && Conditional-AND | OR || Conditional-OR ! NOT ?: Ternary (shorthand for if-then-else statement)
  • 9. ternary ("?:") operator Java supports another conditional operator that is known as the ternary operator "?:" and basically is used for an if-then-else as shorthand as boolean expression ? operand1 : operand2; If we analyze this diagram then we find that, operand1 is returned, if the expression is true; otherwise operand2 is returned in case of false expression.
  • 10. Lets have an example implementing some Logical operators: class ConditionalOperator { public static void main(String[] args) { int x = 5; int y = 10, result=0; boolean bl = true; if((x == 5) && (x < y)) System.out.println("value of x is "+x); if((x == y) || (y > 1)) System.out.println("value of y is greater than the value of x"); result = bl ? x : y; System.out.println("The returned value is "+result); } }
  • 11. Output value of x is 5 is 5 value of y is greater than the value of x lue of y is greater than the value of x The returned value is 5
  • 12. (6) Bitwise and Bit Shift Operators . There are different types of bitwise and bit shift operators available in the Java language summarized in the table. Symbol Name of the Operator Example ~ Unary bitwise complement ~op2 & Bitwise AND op1 & op2 | Bitwise inclusive OR op1 | op2 ^ Bitwise exclusive OR op1 ^ op2 << Signed left shift op1 << op2 >> Signed right sift op1 >> op2 >>> Unsigned right shift op1 >>> op2
  • 13. I. Unary Bitwise Complement ("~") : Lets use the table to understand bitwise complement operation : Operand Result 0 1 1 0 1 0 0 1 II. Bitwise AND (&) : Lets understand the AND operations using truth table: (AND) A B Result 0 0 0 1 0 0 0 1 0 1 1 1
  • 14. III. Bitwise inclusive OR ( | ) : Lets understand the inclusive OR operations using truth table: (OR) A B Result 0 0 0 1 0 1 0 1 1 1 1 1 IV. Bitwise exclusive OR (^) : Lets understand the exclusive OR operations using truth table: A B Result 0 0 0 1 0 1 0 1 1 1 1 0
  • 15.  Bit Shifts Operators: I. Signed Left Shift ("<<") : This diagram shows that, all bits of the upper position were shifted to the left by the distance of 1; and the Zero was shifted to the right most position. Thus the result is returned as 11100. Another expression "2<<2"; shifts all bits of the number 2 to the left placing a zero to the right for each blank place. Thus the value 0010 becomes 1000 or 8 in decimal.
  • 16. II. Signed Right Shift (">>") : This diagram shows that, all bits of the upper position were shifted to the right distance specified by 1; Since the sign bit of this number indicates it as a positive number so the 0 is shifted to the right most position. Thus the result is returned as 00011 or 3 in decimal. Another expression "2>>2"; shifts all bits of the number 2 to the right placing a zero to the left for each blank place. Thus the value 0010 becomes 0000 or 0 in decimal.
  • 17. III. Unsigned Right Shift (">>>") : For example, the expression "14>>>2"; shifts all bits of the number 14 to the right placing a zero to the left for each blank place Thus the value 1110 becomes 0011 or 3 in decimal.
  • 18. Operator Precedence Operators Precedence array index & parentheses [] ( ) access object . postfix expr++ expr-- unary ++expr --expr +expr -expr ~ ! multiplicative * / % additive + - bit shift << >> >>> relational < > <= >= equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || ternary ?: assignment = += -= *= /= %= &= ^= |= <<= >>= >> >=
  • 19.  Lets see an example that evaluates an arithmetic expression according to the precedence order. class PrecedenceDemo { public static void main(String[] args) { int a = 6; int b = 5; int c = 10; float rs = 0; rs = a + (++b)* ((c / a)* b); System.out.println("The result is:" + rs); } }
  • 20.  The expression "a+(++b)*((c/a)*b)" is evaluated from right to left. Its evaluation order depends upon the precedence order of the operators. It is shown below: (++b) a + (++b)*((c/a)*b) (c/a) a+ (++b)*((c/a)*b) (c/a)*b a + (++b)*((c/a)* b) (++b)*((c/a)*b) a + (++b)*((c/a)* b) a+(++b)*((c/a)*b) a+(++b)*((c/a)*b) Output The result is:42.0