SlideShare a Scribd company logo
1 of 26
Operators in Java


Pre-assessment Questions
    1.   Predict the output of the expression, 16 % 3?
         a.   5
         b.   0
         c.   1
         d.   4
    •    What is the default value of the float data type?
         a.   0.0
         b.   0
         c.   1.0
         d.   1




 ©NIIT                    Java Fundamentals                  Lesson 3B / Slide 1 of 26
Operators in Java


Pre-assessment Questions (Contd.)
    3.   Consider the statements:
         Statement A: The name of a variable can begin with a digit.
         Statement B: The name of a variable can contain white spaces.
         Identify the correct option.
         a.   Statement A is true and statement B is false
         b.   Statement A is false and statement B is true
         c.   Both, statements, A and B, are true
         d.   Both, statements, A and B, are false




 ©NIIT                   Java Fundamentals                 Lesson 3B / Slide 2 of 26
Operators in Java


Pre-assessment Questions (Contd.)
    1.   _______variables are the local variables that are accessed by the function in
         which the variables are declared.
         a.   Static
         b.   Automatic
         c.   Instance
         d.   Class

    2.   _______literals are enclosed in single quotes
         a.  String
         b.  Character
         c.  Boolean
         d.  Integer




 ©NIIT                   Java Fundamentals                   Lesson 3B / Slide 3 of 26
Operators in Java


Solutions to Pre-assessment
Questions
    1.   c.
    2.   a.
    3.   d.
    4.   a.
    5.   b.




 ©NIIT         Java Fundamentals   Lesson 3B / Slide 4 of 26
Operators in Java


Objectives
    In this lesson, you will learn to:


         •   Use operators
               • Arithmetic assignment operators
               • bit-wise operators
               • shift operators
               • instance-of operator
         •   Identify the operators precedence




 ©NIIT                     Java Fundamentals       Lesson 3B / Slide 5 of 26
Operators in Java


Using Unary Operators
    •    Using the Increment and Decrement Operators
          • The increment and decrement operators are unary operators.
          • The increment operator (++) increases the value of an operand by 1.
          • The decrement operator (--) decreases the value of an operand by 1.
          • Prefix Form
                • The operator precedes the operand.
                • Operator operates on the operand before the value of operand is
                  used in the expression.
          • Postfix Form
                • In the postfix form, operator follows the operand.
                • Operator operates on the operand after the value of operand is used
                  in the expression.




 ©NIIT                     Java Fundamentals                 Lesson 3B / Slide 6 of 26
Operators in Java


Using the Arithmetic Assignment
Operators
    •    Arithmetic Assignment Operators
           • Addition(+), subtraction(-), multiplication(*), division(/), and modulo(%)
              are the arithmetic operators supported by Java.
           • Various arithmetic operators, such as +, -, /, *, and % are combined with
              the assignment operator (=) and are called arithmetic assignment
              operators.




 ©NIIT                     Java Fundamentals                  Lesson 3B / Slide 7 of 26
Operators in Java


Using the Arithmetic Assignment
Operators (Contd.)
         Operator       Use                       Description



         +=         op1 += op2        Adds operand, op1 and operand, op2
                                      and assigns the result to op1. This
                                      expression is equivalent to op1 =
                                      op1+op2.

         -=         op1 -= op2        Subtracts operand, op2 from
                                      operand, op1 and assigns the result
                                      to op1. This expression is equivalent
                                      to op1 = op1 – op2.


 ©NIIT                 Java Fundamentals                  Lesson 3B / Slide 8 of 26
Operators in Java


Using the Arithmetic Assignment
Operators (Contd.)
         Operator       Use                    Description


     *=             op1 *= op2      Multiplies operand, op1 and
                                    operand, op2 and assigns value of
                                    the result to op1. This expression
                                    is equivalent to op1 = op1*op2.

     /=             op1 /= op2      Divides operand, op1 by operand,
                                    op2, and assign the value of the
                                    result to op1. This expression is
                                    equivalent to op1 = op1/op2.



 ©NIIT                  Java Fundamentals                 Lesson 3B / Slide 9 of 26
Operators in Java


Using the Arithmetic Assignment
Operators (Contd.)


          Operator            Use           Description



         %=            op1 %= op2    Assigns the remainder of
                                     division of operand, op1
                                     and operand, op2 to op1.
                                     This expression is
                                     equivalent to op1 = op1%
                                     op2.


 ©NIIT           Java Fundamentals         Lesson 3B / Slide 10 of 26
Operators in Java


Using Bit-wise Operators
    •    Bit-wise operators
         • Operate on the individual bits of their operand.
         • Operands can be various data types like int, short, long, char, and byte.
         • Operands are converted into their binary equivalents before operation.
         • The result in the binary form after the operation is converted back into
              its decimal equivalent.




 ©NIIT                   Java Fundamentals                Lesson 3B / Slide 11 of 26
Operators in Java


Using Bit-wise Operators(Contd.)
    •    The following table lists the various bit-wise operators in Java:


                Operator           Use                 Operation

                &(AND)           x&y            Performs bit-wise AND
                                                operation. It evaluates to 1 if
                                                both bits, x and y are 1. If
                                                either or both bits are 0, the
                                                result is 0.


                |(OR)            x|y            Performs bit-wise OR
                                                operation. It evaluates to 0 if
                                                both bits, x and y are 0. If
                                                either or both bits are 1, the
                                                result is 1.




 ©NIIT                     Java Fundamentals                    Lesson 3B / Slide 12 of 26
Operators in Java


Using Bit-wise Operators(Contd.)

         Operator          Use                     Operation

         ~(inversion)    ~x                 Performs unary NOT
                                            operation. Converts all the
                                            1s into 0s and all the 0s into
                                            1s.

         ^(XOR)          x^y                Performs bit-wise XOR
                                            operation. It evaluates to 1 if
                                            bits have different values
                                            and 0 if both the bits have
                                            the same value.




 ©NIIT                  Java Fundamentals                    Lesson 3B / Slide 13 of 26
Operators in Java


Using Bit-wise Operators (Contd.)
•      Using the Bit-wise AND Operator
       • The Bit-wise AND operator (&) performs AND operation on two operands.
       • Displays 1 if both bits are 1 else 0 in all other cases.

                         Operation                       Result

                             0&0                             0

                             0&1                             0

                             1&1                             1

                             1&0                             0




    ©NIIT                 Java Fundamentals              Lesson 3B / Slide 14 of 26
Operators in Java


Using Bit-wise Operators (Contd.)
•      Using the Bit-wise OR Operator
       • The Bit-wise OR operator (|) performs OR operation on two operands.
       • Displays 0 if both bits are 0 else 1 in all other cases.

                         Operation                       Result

                              0|0                             0

                              0|1                             1

                              1|1                             1

                              1|0                             1




    ©NIIT                  Java Fundamentals              Lesson 3B / Slide 15 of 26
Operators in Java


Using Bit-wise Operators (Contd.)
•      Using the Bit-wise NOT Operator
       • Bit-wise NOT operator (~) is a unary operator and performs NOT operation
            on each bit of binary number.
       • The NOT operator inverts or complements each of the bits of a binary
            number.


                   Operation                           Result

            ~0                                 1

            ~1                                 0




    ©NIIT                  Java Fundamentals              Lesson 3B / Slide 16 of 26
Operators in Java


Using Bit-wise Operators (Contd.)
•      Using the Bit-wise XOR Operator
       • The Bit-wise XOR (^) operator performs XOR operation on two operands.
       • The XOR operator applied on two bits results in 1 if exactly one bit is 1 else 0
            in all other cases .


                        Operation                           Result

                             0^0                                 0

                             0 ^1                                1

                             1^1                                 0

                             1^0                                 1




    ©NIIT                   Java Fundamentals                 Lesson 3B / Slide 17 of 26
Operators in Java


Using Shift Operators
    •     Works on the bits of data.
    •     Shifts the bits of it’s operand either to left or right.



          Operator                 Use                         Operation

         >>                   val1 >> val2           Shifts the bits of the val1 operand
                                                     to the right by the number of
         (Right Shift)
                                                     positions specified by val2.

         <<                   val1<< val2            Shifts the bits of the val1 operand
                                                     to the left by the number of
         (Left Shift)
                                                     positions specified by val2.




 ©NIIT                      Java Fundamentals                        Lesson 3B / Slide 18 of 26
Operators in Java


Using Shift Operators (Contd.)

     Operator             Use                        Operation

    >>>               val1 >>> val2        Shifts the bits of the val1 operand
                                           to the right by the number of
    (Unsigned Shift
                                           positions specified by val2. A zero
    Operator)
                                           value is input in the high-order bit
                                           irrespective of the value of the
                                           high-order bit of val1. The high-
                                           order bit is the leftmost bit of the
                                           binary number.




 ©NIIT                 Java Fundamentals                   Lesson 3B / Slide 19 of 26
Operators in Java


Using Shift Operators (Contd.)
•      Using the Right Shift and Left Shift Operators
       • The right shift and the left shift operators are binary operators.
            • The right shift operator shifts all the bits of a binary number in the right
                  direction.
                  operand >> num
            • The left shift operator, <<, shifts all the bits of a binary number in the
                  left direction.
                  operand << num
•      Using the Unsigned Shift Operator
       • Unsigned shift operator (>>>) is used to shift the bits of a binary number to
            the right.
       • The operator fills the leftmost bits of a binary value with 0 irrespective of
            whether the number has 0 or 1 at the leftmost bit.




    ©NIIT                    Java Fundamentals                 Lesson 3B / Slide 20 of 26
Operators in Java


Using instance of Operator
    •    Used to test whether an object is an instance of a specific class.
    •    Used at the run time.
    •    The syntax of the instanceof operator is:
         op1 instanceof op2
    •    op1 is the name of an object and op2 is the name of a class.
    •    Returns a true value if the op1 object is an instance of the op2 class.




 ©NIIT                    Java Fundamentals                  Lesson 3B / Slide 21 of 26
Operators in Java


Operators Precedence
    •    Each operator in an expression is evaluated in a predetermined order called
         operator precedence.
    •    Operators on the same line have equal precedence.




                   Operators                              Category

         [], (), . , expr++, expr--             Array index, method call, member
                                                access, and postfix operators

         ++expr, --expr, +, -, !, ~             Unary postfix operators

         *, /, %                                Multiplicative Arithmetic operators

         +, -                                   Additive Arithmetic operators


 ©NIIT                      Java Fundamentals                   Lesson 3B / Slide 22 of 26
Operators in Java


Operators Precedence(Contd.)
                  Operators                            Category

         <<, >>, >>>                         Shift operators

         >, <, <=, >=, instanceof            Relational operators

         ==, !=                              Equality operator

         &                                   Bit-wise AND operator

         ^                                   Bit-wise XOR operator

         |                                   Bit-wise OR operator

         &&                                  Conditional AND operator

         ||                                  Conditional OR operator

         =, +=, -=, *=, /=, %=               Assignment operators

 ©NIIT                   Java Fundamentals                     Lesson 3B / Slide 23 of 26
Operators in Java


Demonstration-Precedence of
Operators in Java
    •    Problem Statement
          • Create a Java application to understand the order of
               precedence of various operators in Java.
    •    Solution
          • The Java application is created using the various operators in
               Java.
          • To solve the above problem, perform the following tasks:
          3. Code an application with an expression having multiple
               operators of different precedence
          4. Compile and execute the code.




 ©NIIT                 Java Fundamentals            Lesson 3B / Slide 24 of 26
Operators in Java


Summary
    •    In this lesson, you learned:
           • Operators are used to calculate, compare and compute values. The
              operators are classified as:
                 • Unary operators
                 • Binary operators
           • Unary operators operate on one operand, such as increment and
              decrement operators.
           • Binary operators operate on two operands, such as arithmetic operators
              +,-,/,*.
           • Arithmetic operators, such as, +, _, combined with assignment
              operator(=) are arithmetic assignment operators.
           • Bit-wise operators operate on one bit at a time.
           • The bit-wise operators operate on data types such as, int, short, long,
              char and byte.
           • The various bit-wise operators are bit-wise AND (&), bit-wise OR (|), bit-
              wise NOT (~), and bit-wise XOR (^).
 ©NIIT                     Java Fundamentals                  Lesson 3B / Slide 25 of 26
Operators in Java


Summary (Contd.)
    •    Shift operators are used to shift the bits of the number to the left or right.
         The various shift operators are right shift (>>), left shift (<<), and unsigned
         shift operator (>>>).
    •    The instanceof operator is used to test that whether an object is an instance
         of a particular class.
    •    An expression can have several operators and each operator is evaluated in
         order of their precedence.




 ©NIIT                    Java Fundamentals                  Lesson 3B / Slide 26 of 26

More Related Content

What's hot

09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statements
Phúc Đỗ
 
03 expressions.ppt
03 expressions.ppt03 expressions.ppt
03 expressions.ppt
Business man
 
High-order Finite Elements for Computational Physics
High-order Finite Elements for Computational PhysicsHigh-order Finite Elements for Computational Physics
High-order Finite Elements for Computational Physics
Robert Rieben
 

What's hot (20)

09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statements
 
03 expressions.ppt
03 expressions.ppt03 expressions.ppt
03 expressions.ppt
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 
FASTEST: Test Case Generation from Z Specifications
FASTEST: Test Case Generation from Z SpecificationsFASTEST: Test Case Generation from Z Specifications
FASTEST: Test Case Generation from Z Specifications
 
Tutorial on Markov Random Fields (MRFs) for Computer Vision Applications
Tutorial on Markov Random Fields (MRFs) for Computer Vision ApplicationsTutorial on Markov Random Fields (MRFs) for Computer Vision Applications
Tutorial on Markov Random Fields (MRFs) for Computer Vision Applications
 
Inference & Learning in Linear Chain Conditional Random Fields (CRFs)
Inference & Learning in Linear Chain Conditional Random Fields (CRFs)Inference & Learning in Linear Chain Conditional Random Fields (CRFs)
Inference & Learning in Linear Chain Conditional Random Fields (CRFs)
 
Thai
ThaiThai
Thai
 
C sharp chap3
C sharp chap3C sharp chap3
C sharp chap3
 
Aaa ped-22-Artificial Neural Network: Introduction to ANN
Aaa ped-22-Artificial Neural Network: Introduction to ANNAaa ped-22-Artificial Neural Network: Introduction to ANN
Aaa ped-22-Artificial Neural Network: Introduction to ANN
 
Aaa ped-23-Artificial Neural Network: Keras and Tensorfow
Aaa ped-23-Artificial Neural Network: Keras and TensorfowAaa ped-23-Artificial Neural Network: Keras and Tensorfow
Aaa ped-23-Artificial Neural Network: Keras and Tensorfow
 
Hg2412901297
Hg2412901297Hg2412901297
Hg2412901297
 
Chapter 07
Chapter 07 Chapter 07
Chapter 07
 
Lecture03(c expressions & operators)
Lecture03(c expressions & operators)Lecture03(c expressions & operators)
Lecture03(c expressions & operators)
 
High-order Finite Elements for Computational Physics
High-order Finite Elements for Computational PhysicsHigh-order Finite Elements for Computational Physics
High-order Finite Elements for Computational Physics
 
Machine learning pt.1: Artificial Neural Networks ® All Rights Reserved
Machine learning pt.1: Artificial Neural Networks ® All Rights ReservedMachine learning pt.1: Artificial Neural Networks ® All Rights Reserved
Machine learning pt.1: Artificial Neural Networks ® All Rights Reserved
 
Numerical analysis using Scilab: Numerical stability and conditioning
Numerical analysis using Scilab: Numerical stability and conditioningNumerical analysis using Scilab: Numerical stability and conditioning
Numerical analysis using Scilab: Numerical stability and conditioning
 
Functions
FunctionsFunctions
Functions
 
Core Java
Core JavaCore Java
Core Java
 
ALGOL ailesi programlama dilleri
ALGOL ailesi programlama dilleriALGOL ailesi programlama dilleri
ALGOL ailesi programlama dilleri
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 

Viewers also liked (13)

02 iec t1_s1_plt_session_02
02 iec t1_s1_plt_session_0202 iec t1_s1_plt_session_02
02 iec t1_s1_plt_session_02
 
Sql xp 11
Sql xp 11Sql xp 11
Sql xp 11
 
Sql xp 10
Sql xp 10Sql xp 10
Sql xp 10
 
02 t1 s2_linux_lesson2
02 t1 s2_linux_lesson202 t1 s2_linux_lesson2
02 t1 s2_linux_lesson2
 
Sql xp 09
Sql xp 09Sql xp 09
Sql xp 09
 
Sql xp 01
Sql xp 01Sql xp 01
Sql xp 01
 
Comp tia n+_session_08
Comp tia n+_session_08Comp tia n+_session_08
Comp tia n+_session_08
 
01 iec t1_s1_plt_session_01
01 iec t1_s1_plt_session_0101 iec t1_s1_plt_session_01
01 iec t1_s1_plt_session_01
 
SQL | Computer Science
SQL | Computer ScienceSQL | Computer Science
SQL | Computer Science
 
MS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTUREMS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTURE
 
Sql server basics
Sql server basicsSql server basics
Sql server basics
 
Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architecture
 

Similar to Dacj 1-3 b

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
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
CtOlaf
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
AjayBahoriya
 

Similar to Dacj 1-3 b (20)

Report on c
Report on cReport on c
Report on c
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.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 in java By cheena
Operators in java By cheenaOperators in java By cheena
Operators in java By cheena
 
Shubhrat operator &amp; expression
Shubhrat operator &amp; expressionShubhrat operator &amp; expression
Shubhrat operator &amp; expression
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
operat in vb .pptx
operat in vb .pptxoperat in vb .pptx
operat in vb .pptx
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
Operators
OperatorsOperators
Operators
 
Std 12 Computer Chapter 7 Java Basics (Part 2)
Std 12 Computer Chapter 7 Java Basics (Part 2)Std 12 Computer Chapter 7 Java Basics (Part 2)
Std 12 Computer Chapter 7 Java Basics (Part 2)
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
OCA JAVA - 3 Programming with Java Operators
 OCA JAVA - 3 Programming with Java Operators OCA JAVA - 3 Programming with Java Operators
OCA JAVA - 3 Programming with Java Operators
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Operators-WPS Office.pdf
Operators-WPS Office.pdfOperators-WPS Office.pdf
Operators-WPS Office.pdf
 

More from Niit Care (20)

Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
 

Recently uploaded

Recently uploaded (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Dacj 1-3 b

  • 1. Operators in Java Pre-assessment Questions 1. Predict the output of the expression, 16 % 3? a. 5 b. 0 c. 1 d. 4 • What is the default value of the float data type? a. 0.0 b. 0 c. 1.0 d. 1 ©NIIT Java Fundamentals Lesson 3B / Slide 1 of 26
  • 2. Operators in Java Pre-assessment Questions (Contd.) 3. Consider the statements: Statement A: The name of a variable can begin with a digit. Statement B: The name of a variable can contain white spaces. Identify the correct option. a. Statement A is true and statement B is false b. Statement A is false and statement B is true c. Both, statements, A and B, are true d. Both, statements, A and B, are false ©NIIT Java Fundamentals Lesson 3B / Slide 2 of 26
  • 3. Operators in Java Pre-assessment Questions (Contd.) 1. _______variables are the local variables that are accessed by the function in which the variables are declared. a. Static b. Automatic c. Instance d. Class 2. _______literals are enclosed in single quotes a. String b. Character c. Boolean d. Integer ©NIIT Java Fundamentals Lesson 3B / Slide 3 of 26
  • 4. Operators in Java Solutions to Pre-assessment Questions 1. c. 2. a. 3. d. 4. a. 5. b. ©NIIT Java Fundamentals Lesson 3B / Slide 4 of 26
  • 5. Operators in Java Objectives In this lesson, you will learn to: • Use operators • Arithmetic assignment operators • bit-wise operators • shift operators • instance-of operator • Identify the operators precedence ©NIIT Java Fundamentals Lesson 3B / Slide 5 of 26
  • 6. Operators in Java Using Unary Operators • Using the Increment and Decrement Operators • The increment and decrement operators are unary operators. • The increment operator (++) increases the value of an operand by 1. • The decrement operator (--) decreases the value of an operand by 1. • Prefix Form • The operator precedes the operand. • Operator operates on the operand before the value of operand is used in the expression. • Postfix Form • In the postfix form, operator follows the operand. • Operator operates on the operand after the value of operand is used in the expression. ©NIIT Java Fundamentals Lesson 3B / Slide 6 of 26
  • 7. Operators in Java Using the Arithmetic Assignment Operators • Arithmetic Assignment Operators • Addition(+), subtraction(-), multiplication(*), division(/), and modulo(%) are the arithmetic operators supported by Java. • Various arithmetic operators, such as +, -, /, *, and % are combined with the assignment operator (=) and are called arithmetic assignment operators. ©NIIT Java Fundamentals Lesson 3B / Slide 7 of 26
  • 8. Operators in Java Using the Arithmetic Assignment Operators (Contd.) Operator Use Description += op1 += op2 Adds operand, op1 and operand, op2 and assigns the result to op1. This expression is equivalent to op1 = op1+op2. -= op1 -= op2 Subtracts operand, op2 from operand, op1 and assigns the result to op1. This expression is equivalent to op1 = op1 – op2. ©NIIT Java Fundamentals Lesson 3B / Slide 8 of 26
  • 9. Operators in Java Using the Arithmetic Assignment Operators (Contd.) Operator Use Description *= op1 *= op2 Multiplies operand, op1 and operand, op2 and assigns value of the result to op1. This expression is equivalent to op1 = op1*op2. /= op1 /= op2 Divides operand, op1 by operand, op2, and assign the value of the result to op1. This expression is equivalent to op1 = op1/op2. ©NIIT Java Fundamentals Lesson 3B / Slide 9 of 26
  • 10. Operators in Java Using the Arithmetic Assignment Operators (Contd.) Operator Use Description %= op1 %= op2 Assigns the remainder of division of operand, op1 and operand, op2 to op1. This expression is equivalent to op1 = op1% op2. ©NIIT Java Fundamentals Lesson 3B / Slide 10 of 26
  • 11. Operators in Java Using Bit-wise Operators • Bit-wise operators • Operate on the individual bits of their operand. • Operands can be various data types like int, short, long, char, and byte. • Operands are converted into their binary equivalents before operation. • The result in the binary form after the operation is converted back into its decimal equivalent. ©NIIT Java Fundamentals Lesson 3B / Slide 11 of 26
  • 12. Operators in Java Using Bit-wise Operators(Contd.) • The following table lists the various bit-wise operators in Java: Operator Use Operation &(AND) x&y Performs bit-wise AND operation. It evaluates to 1 if both bits, x and y are 1. If either or both bits are 0, the result is 0. |(OR) x|y Performs bit-wise OR operation. It evaluates to 0 if both bits, x and y are 0. If either or both bits are 1, the result is 1. ©NIIT Java Fundamentals Lesson 3B / Slide 12 of 26
  • 13. Operators in Java Using Bit-wise Operators(Contd.) Operator Use Operation ~(inversion) ~x Performs unary NOT operation. Converts all the 1s into 0s and all the 0s into 1s. ^(XOR) x^y Performs bit-wise XOR operation. It evaluates to 1 if bits have different values and 0 if both the bits have the same value. ©NIIT Java Fundamentals Lesson 3B / Slide 13 of 26
  • 14. Operators in Java Using Bit-wise Operators (Contd.) • Using the Bit-wise AND Operator • The Bit-wise AND operator (&) performs AND operation on two operands. • Displays 1 if both bits are 1 else 0 in all other cases. Operation Result 0&0 0 0&1 0 1&1 1 1&0 0 ©NIIT Java Fundamentals Lesson 3B / Slide 14 of 26
  • 15. Operators in Java Using Bit-wise Operators (Contd.) • Using the Bit-wise OR Operator • The Bit-wise OR operator (|) performs OR operation on two operands. • Displays 0 if both bits are 0 else 1 in all other cases. Operation Result 0|0 0 0|1 1 1|1 1 1|0 1 ©NIIT Java Fundamentals Lesson 3B / Slide 15 of 26
  • 16. Operators in Java Using Bit-wise Operators (Contd.) • Using the Bit-wise NOT Operator • Bit-wise NOT operator (~) is a unary operator and performs NOT operation on each bit of binary number. • The NOT operator inverts or complements each of the bits of a binary number. Operation Result ~0 1 ~1 0 ©NIIT Java Fundamentals Lesson 3B / Slide 16 of 26
  • 17. Operators in Java Using Bit-wise Operators (Contd.) • Using the Bit-wise XOR Operator • The Bit-wise XOR (^) operator performs XOR operation on two operands. • The XOR operator applied on two bits results in 1 if exactly one bit is 1 else 0 in all other cases . Operation Result 0^0 0 0 ^1 1 1^1 0 1^0 1 ©NIIT Java Fundamentals Lesson 3B / Slide 17 of 26
  • 18. Operators in Java Using Shift Operators • Works on the bits of data. • Shifts the bits of it’s operand either to left or right. Operator Use Operation >> val1 >> val2 Shifts the bits of the val1 operand to the right by the number of (Right Shift) positions specified by val2. << val1<< val2 Shifts the bits of the val1 operand to the left by the number of (Left Shift) positions specified by val2. ©NIIT Java Fundamentals Lesson 3B / Slide 18 of 26
  • 19. Operators in Java Using Shift Operators (Contd.) Operator Use Operation >>> val1 >>> val2 Shifts the bits of the val1 operand to the right by the number of (Unsigned Shift positions specified by val2. A zero Operator) value is input in the high-order bit irrespective of the value of the high-order bit of val1. The high- order bit is the leftmost bit of the binary number. ©NIIT Java Fundamentals Lesson 3B / Slide 19 of 26
  • 20. Operators in Java Using Shift Operators (Contd.) • Using the Right Shift and Left Shift Operators • The right shift and the left shift operators are binary operators. • The right shift operator shifts all the bits of a binary number in the right direction. operand >> num • The left shift operator, <<, shifts all the bits of a binary number in the left direction. operand << num • Using the Unsigned Shift Operator • Unsigned shift operator (>>>) is used to shift the bits of a binary number to the right. • The operator fills the leftmost bits of a binary value with 0 irrespective of whether the number has 0 or 1 at the leftmost bit. ©NIIT Java Fundamentals Lesson 3B / Slide 20 of 26
  • 21. Operators in Java Using instance of Operator • Used to test whether an object is an instance of a specific class. • Used at the run time. • The syntax of the instanceof operator is: op1 instanceof op2 • op1 is the name of an object and op2 is the name of a class. • Returns a true value if the op1 object is an instance of the op2 class. ©NIIT Java Fundamentals Lesson 3B / Slide 21 of 26
  • 22. Operators in Java Operators Precedence • Each operator in an expression is evaluated in a predetermined order called operator precedence. • Operators on the same line have equal precedence. Operators Category [], (), . , expr++, expr-- Array index, method call, member access, and postfix operators ++expr, --expr, +, -, !, ~ Unary postfix operators *, /, % Multiplicative Arithmetic operators +, - Additive Arithmetic operators ©NIIT Java Fundamentals Lesson 3B / Slide 22 of 26
  • 23. Operators in Java Operators Precedence(Contd.) Operators Category <<, >>, >>> Shift operators >, <, <=, >=, instanceof Relational operators ==, != Equality operator & Bit-wise AND operator ^ Bit-wise XOR operator | Bit-wise OR operator && Conditional AND operator || Conditional OR operator =, +=, -=, *=, /=, %= Assignment operators ©NIIT Java Fundamentals Lesson 3B / Slide 23 of 26
  • 24. Operators in Java Demonstration-Precedence of Operators in Java • Problem Statement • Create a Java application to understand the order of precedence of various operators in Java. • Solution • The Java application is created using the various operators in Java. • To solve the above problem, perform the following tasks: 3. Code an application with an expression having multiple operators of different precedence 4. Compile and execute the code. ©NIIT Java Fundamentals Lesson 3B / Slide 24 of 26
  • 25. Operators in Java Summary • In this lesson, you learned: • Operators are used to calculate, compare and compute values. The operators are classified as: • Unary operators • Binary operators • Unary operators operate on one operand, such as increment and decrement operators. • Binary operators operate on two operands, such as arithmetic operators +,-,/,*. • Arithmetic operators, such as, +, _, combined with assignment operator(=) are arithmetic assignment operators. • Bit-wise operators operate on one bit at a time. • The bit-wise operators operate on data types such as, int, short, long, char and byte. • The various bit-wise operators are bit-wise AND (&), bit-wise OR (|), bit- wise NOT (~), and bit-wise XOR (^). ©NIIT Java Fundamentals Lesson 3B / Slide 25 of 26
  • 26. Operators in Java Summary (Contd.) • Shift operators are used to shift the bits of the number to the left or right. The various shift operators are right shift (>>), left shift (<<), and unsigned shift operator (>>>). • The instanceof operator is used to test that whether an object is an instance of a particular class. • An expression can have several operators and each operator is evaluated in order of their precedence. ©NIIT Java Fundamentals Lesson 3B / Slide 26 of 26