SlideShare a Scribd company logo
1 of 20
Collaborate


Knowledge Byte
    In this section, you will learn about:


         •   Casting and conversion in Java
         •   Overloading constructors




 ©NIIT                          Collaborate   Lesson 3C / Slide 1 of 22
Collaborate


Casting and Conversion in Java
•    Java supports implicit conversion of one data type to another type. are inbuilt in
     Java.
•    Implicit conversions are the conversions from one data type to another, which
     occur automatically in a program.
•    For example, you can assign a value of int type to a variable of long data type.
•    When you assign a value of a particular data type to another variable of a different
     data type, the two types must be compatible with each other.
•    The two data types are compatible to each other if the size of the destination data
     type variable is larger than or equal to the size of the source data type variable.
•    Widening conversion takes place when the destination type is greater than the
     source type.
•    Assigning a wider type to a narrow type is known as narrowing conversion.
•    The widening conversion is implicit while the narrowing conversions are explicit.




    ©NIIT                        Collaborate                    Lesson 3C / Slide 2 of 22
Collaborate


Casting and Conversion in Java
(Contd.)
•    Explicit conversion occurs when one data type cannot be assigned to another data

    type using implicit conversion.
•   In an explicit conversion, you must convert the data type to the compatible type.
•   Explicit conversion between incompatible data types is known as casting.
•   The following syntax shows how to use a cast to perform conversion between two
     incompatible types:
            (type) value
•   Explicit conversion between incompatible data types is known as casting.




    ©NIIT                        Collaborate                  Lesson 3C / Slide 3 of 22
Collaborate


Casting and Conversion in Java
(Contd.)
    •    You can use the following code to perform type casting of an int number, 259
         and a double number, 350.55 to byte type:
         class TypeCast
         {
           public static void main(String arr[])
           {
                    byte b;
                    int i = 259;
                    double d = 350.55;
                    b = (byte) i;
                    System.out.println("Value of int to byte conversion " +
         b);


 ©NIIT                          Collaborate                  Lesson 3C / Slide 4 of 22
Collaborate


Casting and Conversion in Java
(Contd.)
    b = (byte) d;
        System.out.println("Value of double to byte conversion " + b);

             i = (int) d;
             System.out.println("Value of double to int conversion " + i);

         }
    }




 ©NIIT                         Collaborate              Lesson 3C / Slide 5 of 22
Collaborate


Overloading Constructors
    •    A constructor is a method that is automatically invoked every time an
         instance of a class is created.
    •    Constructors share the same name as the class name and do not have a
         return type.
    •    You can use the following code to overload the Cuboid() constructor to
         calculate the volume of a rectangle and a square:
         class Cuboid
          {
                   double length;
                   double width;
                   double height;




 ©NIIT                       Collaborate                  Lesson 3C / Slide 6 of 22
Collaborate


Overloading Constructors (Contd.)
// Constructor declared which accepts three arguments
         Cuboid(double l, double w, double h)
         {
                  length = l;
                  width = w;
                  height = h;
         }
         // Overloaded constructor declared which accepts one argument
         Cuboid(double side)
         {
                  length = width = height = side;
         }
         double volume()
         {
                  return length*width*height;
         }
}



 ©NIIT                     Collaborate              Lesson 3C / Slide 7 of 22
Collaborate


Overloading Constructors (Contd.)
class ConstrOverloading
{
         public static void main(String args[])
         {
                  Cuboid cub1 = new Cuboid(5, 10, 15);
                  Cuboid cub2 = new Cuboid(5);
                  double vol;

                 vol = cub1.volume();
                 System.out.println("Volume of the Cuboid is: "+ vol);
                 vol = cub2.volume();
                 System.out.println("Volume of the Cube is :" + vol);
            }
}




    ©NIIT                  Collaborate               Lesson 3C / Slide 8 of 22
Collaborate


From the Expert’s Desk

    In this section, you will learn:


         •    Best practices on:
              • Short-circuiting
              • Use of switch-case and if-else construct
              • Using the for loop construct
              • Using brackets with the if statement
         •    Tips and Tricks on:
              • Ternary Operator
         •    FAQs on Java Constructs and Operators




 ©NIIT                           Collaborate               Lesson 3C / Slide 9 of 22
Collaborate


Best Practices
Short-circuiting
•      Short-circuiting is the process in which a compiler evaluates and determines the
       result of an expression without evaluating the complete expression but by partial
       evaluation of one of the operands.
•      Java supports conditional operators, such as AND (&&) and OR (||), which are
       also called short-circuit operators.
•      For example, the expression, op1 && op2 && op3 evaluates true only if all the
       operands, op1, op2, and op3 are true.




    ©NIIT                        Collaborate                  Lesson 3C / Slide 10 of 22
Collaborate


Best Practices
Short-circuiting (Contd.)
•      You can use the following code snippet to print a result if both of the conditions
       specified by the && operator are true:
       if(3<5 && 3>2)
       System.out.println(“Result”);
       else
       System.out.println(“No result”);
•      You can use the following code snippet to use && operator as a short circuit
       operator.
       if(3>5 && 3>2)
       System.out.println(“Result”);
       else
       System.out.println(“No result”);


    ©NIIT                         Collaborate                   Lesson 3C / Slide 11 of 22
Collaborate


Best Practices
Use of switch-case and if-else Construct
•      When you have multiple options and one choice, you use the switch-case
       construct.
•      When you have multiple conditions out of which only one condition can be true,
       you use the nested if-else construct.




    ©NIIT                        Collaborate                 Lesson 3C / Slide 12 of 22
Collaborate


Best Practices
Using the for Loop Construct
•      When the control variable of the looping statement is of int type and the number
       of times a loop should execute is known in advance, you use the, for, loop. In the,
       for, loop, all the three parts, such as initialization, condition, and increment or
       decrement are defined initially at one place.




    ©NIIT                         Collaborate                  Lesson 3C / Slide 13 of 22
Collaborate


Best Practices
Using Brackets with the if Statement
•      You should use opening and closing brackets with the if statement.
•      When the if block consists of more than one statement, place the statements in
       brackets.
•      If the if block consists of only one statement, it is not necessary to place single
       statement in brackets.
•      However, it is advisable to place even a single statement in brackets because
       brackets enhance the code clarity and minimize the chances of errors.




    ©NIIT                         Collaborate                   Lesson 3C / Slide 14 of 22
Collaborate


Tips and Tricks
Ternary operator

•      The conditional operator (?:) operates on three operands and is therefore called
       the ternary operator.
•      The syntax of the ternary operator is:
       (boolean_expr) ? val1 : val2
•      Ternary operator is used to replace the if-else statements.
•      You can use the following code snippet to display the grade scored by a student
       using the ternary operator:
       Score = 95;
       (Score>=90) ? System.out.println(“Grade is A.”); :
            System.out.println(“Grade is B.”);




    ©NIIT                        Collaborate                  Lesson 3C / Slide 15 of 22
Collaborate


FAQs
    •    Can you use a switch statement inside another switch statement?

         Yes, you can use a switch statement within another switch statement. This is
         called nested switch. Each switch statement creates its own block of case
         statements. Therefore, no conflict occurs between the case labels of the
         inner and outer switch statements .


    •    How does the program stop running if a loop never ends?

         A program stops running when it enters an infinite loop by pressing the keys
         Ctrl and C simultaneously .




 ©NIIT                        Collaborate                  Lesson 3C / Slide 16 of 22
Collaborate


FAQs (Contd.)
    •    Can you add the numeric value of one string to the value of another if the +
         operator is used with strings to link up two different strings?

         Yes, you can use the value of a String variable as an integer only by using a
         method that converts the value of the string variable into a numeric form.
         This is known as type casting because it casts one data type, such as a string
         into another data type, such as int.


    •    Can the == operator be used to determine whether two strings have the
         same value as in name == "John"?

         Yes, the == operator can be used to determine whether two strings have the
         same value. In Java, you can also compare two strings using the equals()
         method.



 ©NIIT                         Collaborate                  Lesson 3C / Slide 17 of 22
Collaborate


Challenge
    1.   Match the following:
         a.    ++                            i. Logical AND
         b.    &&                           ii. Logical OR
         c.    %                           iii. Logical NOT
         d.    &                           iv. Unsigned Shift
         e.    ||                           v. Bit-wise XOR
         f.    >>>                         vi. Arithmetic Assignment
         g.    !                          vii. Unary Increment
         h.    ^                          viii. Bit-wise AND
         i.    +=                          ix. Ternary
         j.    ?                            x. Modulus
    2. The if decision construct is used when there are multiple values for the same
       variable. (True/False).



 ©NIIT                         Collaborate                  Lesson 3C / Slide 18 of 22
Collaborate


Challenge (Contd.)
    1.   Predict the output of the following code snippet
         int n=5, d=4;
         int remainder = n % d;
         if (div != 0)
         System.out.println(n + “ is not completely divisible by ” + d);
         else
         System.out.println(n + “ is completely divisible by ” + d);

         a) 5 is not completely divisible by 4
         b) 5 is completely divisible by 4




 ©NIIT                         Collaborate         Lesson 3C / Slide 19 of 22
Collaborate


Solutions to Challenge
    •    a-vii, b-i, c-x, d-viii, e-ii, f-iv, g-iii, h-v, i-vi, j-ix
    •    False
    •    a 5 is not completely divisible by 4




 ©NIIT                     Collaborate             Lesson 3C / Slide 20 of 22

More Related Content

What's hot

50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questionsSynergisticMedia
 
10 iec t1_s1_oo_ps_session_14
10 iec t1_s1_oo_ps_session_1410 iec t1_s1_oo_ps_session_14
10 iec t1_s1_oo_ps_session_14Niit Care
 
12 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_1712 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_17Niit Care
 
03 iec t1_s1_oo_ps_session_04
03 iec t1_s1_oo_ps_session_0403 iec t1_s1_oo_ps_session_04
03 iec t1_s1_oo_ps_session_04Niit Care
 
11 iec t1_s1_oo_ps_session_16
11 iec t1_s1_oo_ps_session_1611 iec t1_s1_oo_ps_session_16
11 iec t1_s1_oo_ps_session_16Niit Care
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interviewKuntal Bhowmick
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questionsRohit Singh
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetdevlabsalliance
 
Introduction to the Java(TM) Advanced Imaging API
Introduction to the Java(TM) Advanced Imaging APIIntroduction to the Java(TM) Advanced Imaging API
Introduction to the Java(TM) Advanced Imaging APIwhite paper
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview QuestionsEhtisham Ali
 
Design pattern application
Design pattern applicationDesign pattern application
Design pattern applicationgayatri thakur
 
9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot missMark Papis
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreadingKuntal Bhowmick
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview QestionsArun Vasanth
 
13 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_1913 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_19Niit Care
 

What's hot (19)

50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questions
 
10 iec t1_s1_oo_ps_session_14
10 iec t1_s1_oo_ps_session_1410 iec t1_s1_oo_ps_session_14
10 iec t1_s1_oo_ps_session_14
 
12 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_1712 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_17
 
03 iec t1_s1_oo_ps_session_04
03 iec t1_s1_oo_ps_session_0403 iec t1_s1_oo_ps_session_04
03 iec t1_s1_oo_ps_session_04
 
11 iec t1_s1_oo_ps_session_16
11 iec t1_s1_oo_ps_session_1611 iec t1_s1_oo_ps_session_16
11 iec t1_s1_oo_ps_session_16
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdet
 
Java interview question
Java interview questionJava interview question
Java interview question
 
Best interview questions
Best interview questionsBest interview questions
Best interview questions
 
Introduction to the Java(TM) Advanced Imaging API
Introduction to the Java(TM) Advanced Imaging APIIntroduction to the Java(TM) Advanced Imaging API
Introduction to the Java(TM) Advanced Imaging API
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
 
Design pattern application
Design pattern applicationDesign pattern application
Design pattern application
 
9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 
Java
JavaJava
Java
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
13 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_1913 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_19
 

Viewers also liked

09 intel v_tune_session_13
09 intel v_tune_session_1309 intel v_tune_session_13
09 intel v_tune_session_13Niit Care
 
10 intel v_tune_session_14
10 intel  v_tune_session_1410 intel  v_tune_session_14
10 intel v_tune_session_14Niit Care
 
NIIT Presentation 1
NIIT Presentation 1NIIT Presentation 1
NIIT Presentation 1NIIT Bahrain
 

Viewers also liked (7)

09 intel v_tune_session_13
09 intel v_tune_session_1309 intel v_tune_session_13
09 intel v_tune_session_13
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-1 c
Dacj 1-1 cDacj 1-1 c
Dacj 1-1 c
 
10 intel v_tune_session_14
10 intel  v_tune_session_1410 intel  v_tune_session_14
10 intel v_tune_session_14
 
NIIT Presentation 1
NIIT Presentation 1NIIT Presentation 1
NIIT Presentation 1
 
Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 

Similar to Dacj 1-3 c

Similar to Dacj 1-3 c (20)

Dacj 2-1 c
Dacj 2-1 cDacj 2-1 c
Dacj 2-1 c
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Csharp
CsharpCsharp
Csharp
 
Dacj 2-2 c
Dacj 2-2 cDacj 2-2 c
Dacj 2-2 c
 
Aspdot
AspdotAspdot
Aspdot
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
VB Script Overview
VB Script OverviewVB Script Overview
VB Script Overview
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
 
Core java
Core javaCore java
Core java
 
Core java
Core javaCore java
Core java
 
Gae icc fall2011
Gae icc fall2011Gae icc fall2011
Gae icc fall2011
 
Test design techniques
Test design techniquesTest design techniques
Test design techniques
 
Lecture1
Lecture1Lecture1
Lecture1
 
Lesson 39
Lesson 39Lesson 39
Lesson 39
 
AI Lesson 39
AI Lesson 39AI Lesson 39
AI Lesson 39
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
 
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 

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 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 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 
Dacj 1-3 a
Dacj 1-3 aDacj 1-3 a
Dacj 1-3 a
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
 
Dacj 1-1 a
Dacj 1-1 aDacj 1-1 a
Dacj 1-1 a
 

Recently uploaded

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Dacj 1-3 c

  • 1. Collaborate Knowledge Byte In this section, you will learn about: • Casting and conversion in Java • Overloading constructors ©NIIT Collaborate Lesson 3C / Slide 1 of 22
  • 2. Collaborate Casting and Conversion in Java • Java supports implicit conversion of one data type to another type. are inbuilt in Java. • Implicit conversions are the conversions from one data type to another, which occur automatically in a program. • For example, you can assign a value of int type to a variable of long data type. • When you assign a value of a particular data type to another variable of a different data type, the two types must be compatible with each other. • The two data types are compatible to each other if the size of the destination data type variable is larger than or equal to the size of the source data type variable. • Widening conversion takes place when the destination type is greater than the source type. • Assigning a wider type to a narrow type is known as narrowing conversion. • The widening conversion is implicit while the narrowing conversions are explicit. ©NIIT Collaborate Lesson 3C / Slide 2 of 22
  • 3. Collaborate Casting and Conversion in Java (Contd.) • Explicit conversion occurs when one data type cannot be assigned to another data type using implicit conversion. • In an explicit conversion, you must convert the data type to the compatible type. • Explicit conversion between incompatible data types is known as casting. • The following syntax shows how to use a cast to perform conversion between two incompatible types: (type) value • Explicit conversion between incompatible data types is known as casting. ©NIIT Collaborate Lesson 3C / Slide 3 of 22
  • 4. Collaborate Casting and Conversion in Java (Contd.) • You can use the following code to perform type casting of an int number, 259 and a double number, 350.55 to byte type: class TypeCast { public static void main(String arr[]) { byte b; int i = 259; double d = 350.55; b = (byte) i; System.out.println("Value of int to byte conversion " + b); ©NIIT Collaborate Lesson 3C / Slide 4 of 22
  • 5. Collaborate Casting and Conversion in Java (Contd.) b = (byte) d; System.out.println("Value of double to byte conversion " + b); i = (int) d; System.out.println("Value of double to int conversion " + i); } } ©NIIT Collaborate Lesson 3C / Slide 5 of 22
  • 6. Collaborate Overloading Constructors • A constructor is a method that is automatically invoked every time an instance of a class is created. • Constructors share the same name as the class name and do not have a return type. • You can use the following code to overload the Cuboid() constructor to calculate the volume of a rectangle and a square: class Cuboid { double length; double width; double height; ©NIIT Collaborate Lesson 3C / Slide 6 of 22
  • 7. Collaborate Overloading Constructors (Contd.) // Constructor declared which accepts three arguments Cuboid(double l, double w, double h) { length = l; width = w; height = h; } // Overloaded constructor declared which accepts one argument Cuboid(double side) { length = width = height = side; } double volume() { return length*width*height; } } ©NIIT Collaborate Lesson 3C / Slide 7 of 22
  • 8. Collaborate Overloading Constructors (Contd.) class ConstrOverloading { public static void main(String args[]) { Cuboid cub1 = new Cuboid(5, 10, 15); Cuboid cub2 = new Cuboid(5); double vol; vol = cub1.volume(); System.out.println("Volume of the Cuboid is: "+ vol); vol = cub2.volume(); System.out.println("Volume of the Cube is :" + vol); } } ©NIIT Collaborate Lesson 3C / Slide 8 of 22
  • 9. Collaborate From the Expert’s Desk In this section, you will learn: • Best practices on: • Short-circuiting • Use of switch-case and if-else construct • Using the for loop construct • Using brackets with the if statement • Tips and Tricks on: • Ternary Operator • FAQs on Java Constructs and Operators ©NIIT Collaborate Lesson 3C / Slide 9 of 22
  • 10. Collaborate Best Practices Short-circuiting • Short-circuiting is the process in which a compiler evaluates and determines the result of an expression without evaluating the complete expression but by partial evaluation of one of the operands. • Java supports conditional operators, such as AND (&&) and OR (||), which are also called short-circuit operators. • For example, the expression, op1 && op2 && op3 evaluates true only if all the operands, op1, op2, and op3 are true. ©NIIT Collaborate Lesson 3C / Slide 10 of 22
  • 11. Collaborate Best Practices Short-circuiting (Contd.) • You can use the following code snippet to print a result if both of the conditions specified by the && operator are true: if(3<5 && 3>2) System.out.println(“Result”); else System.out.println(“No result”); • You can use the following code snippet to use && operator as a short circuit operator. if(3>5 && 3>2) System.out.println(“Result”); else System.out.println(“No result”); ©NIIT Collaborate Lesson 3C / Slide 11 of 22
  • 12. Collaborate Best Practices Use of switch-case and if-else Construct • When you have multiple options and one choice, you use the switch-case construct. • When you have multiple conditions out of which only one condition can be true, you use the nested if-else construct. ©NIIT Collaborate Lesson 3C / Slide 12 of 22
  • 13. Collaborate Best Practices Using the for Loop Construct • When the control variable of the looping statement is of int type and the number of times a loop should execute is known in advance, you use the, for, loop. In the, for, loop, all the three parts, such as initialization, condition, and increment or decrement are defined initially at one place. ©NIIT Collaborate Lesson 3C / Slide 13 of 22
  • 14. Collaborate Best Practices Using Brackets with the if Statement • You should use opening and closing brackets with the if statement. • When the if block consists of more than one statement, place the statements in brackets. • If the if block consists of only one statement, it is not necessary to place single statement in brackets. • However, it is advisable to place even a single statement in brackets because brackets enhance the code clarity and minimize the chances of errors. ©NIIT Collaborate Lesson 3C / Slide 14 of 22
  • 15. Collaborate Tips and Tricks Ternary operator • The conditional operator (?:) operates on three operands and is therefore called the ternary operator. • The syntax of the ternary operator is: (boolean_expr) ? val1 : val2 • Ternary operator is used to replace the if-else statements. • You can use the following code snippet to display the grade scored by a student using the ternary operator: Score = 95; (Score>=90) ? System.out.println(“Grade is A.”); : System.out.println(“Grade is B.”); ©NIIT Collaborate Lesson 3C / Slide 15 of 22
  • 16. Collaborate FAQs • Can you use a switch statement inside another switch statement? Yes, you can use a switch statement within another switch statement. This is called nested switch. Each switch statement creates its own block of case statements. Therefore, no conflict occurs between the case labels of the inner and outer switch statements . • How does the program stop running if a loop never ends? A program stops running when it enters an infinite loop by pressing the keys Ctrl and C simultaneously . ©NIIT Collaborate Lesson 3C / Slide 16 of 22
  • 17. Collaborate FAQs (Contd.) • Can you add the numeric value of one string to the value of another if the + operator is used with strings to link up two different strings? Yes, you can use the value of a String variable as an integer only by using a method that converts the value of the string variable into a numeric form. This is known as type casting because it casts one data type, such as a string into another data type, such as int. • Can the == operator be used to determine whether two strings have the same value as in name == "John"? Yes, the == operator can be used to determine whether two strings have the same value. In Java, you can also compare two strings using the equals() method. ©NIIT Collaborate Lesson 3C / Slide 17 of 22
  • 18. Collaborate Challenge 1. Match the following: a. ++ i. Logical AND b. && ii. Logical OR c. % iii. Logical NOT d. & iv. Unsigned Shift e. || v. Bit-wise XOR f. >>> vi. Arithmetic Assignment g. ! vii. Unary Increment h. ^ viii. Bit-wise AND i. += ix. Ternary j. ? x. Modulus 2. The if decision construct is used when there are multiple values for the same variable. (True/False). ©NIIT Collaborate Lesson 3C / Slide 18 of 22
  • 19. Collaborate Challenge (Contd.) 1. Predict the output of the following code snippet int n=5, d=4; int remainder = n % d; if (div != 0) System.out.println(n + “ is not completely divisible by ” + d); else System.out.println(n + “ is completely divisible by ” + d); a) 5 is not completely divisible by 4 b) 5 is completely divisible by 4 ©NIIT Collaborate Lesson 3C / Slide 19 of 22
  • 20. Collaborate Solutions to Challenge • a-vii, b-i, c-x, d-viii, e-ii, f-iv, g-iii, h-v, i-vi, j-ix • False • a 5 is not completely divisible by 4 ©NIIT Collaborate Lesson 3C / Slide 20 of 22