SlideShare una empresa de Scribd logo
1 de 21
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 - Operators

Más contenido relacionado

La actualidad más candente

Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
vishaljot_kaur
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
Pranav Ghildiyal
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
vinay arora
 
Python Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inPython Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.in
Learnbayin
 

La actualidad más candente (20)

Operators In Java Part - 8
Operators In Java Part - 8Operators In Java Part - 8
Operators In Java Part - 8
 
Java 2
Java 2Java 2
Java 2
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
Java operators
Java operatorsJava operators
Java operators
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Python Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inPython Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.in
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
operators in c++
operators in c++operators in c++
operators in c++
 

Destacado (11)

15 bitwise operators
15 bitwise operators15 bitwise operators
15 bitwise operators
 
Java Datatypes
Java DatatypesJava Datatypes
Java Datatypes
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Slides cloud computing
Slides cloud computingSlides cloud computing
Slides cloud computing
 
Cloud computing simple ppt
Cloud computing simple pptCloud computing simple ppt
Cloud computing simple ppt
 
Type conversions
Type conversionsType conversions
Type conversions
 
Introduction of Cloud computing
Introduction of Cloud computingIntroduction of Cloud computing
Introduction of Cloud computing
 
cloud computing ppt
cloud computing pptcloud computing ppt
cloud computing ppt
 

Similar a Java - Operators

Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
CtOlaf
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
jahanullah
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
웅식 전
 

Similar a Java - Operators (20)

Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
 
Report on c
Report on cReport on c
Report on c
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
Operators
OperatorsOperators
Operators
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Operators
OperatorsOperators
Operators
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
 
C++ Expressions Notes
C++ Expressions NotesC++ Expressions Notes
C++ Expressions Notes
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
Operators
OperatorsOperators
Operators
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 

Java - Operators

  • 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