SlideShare a Scribd company logo
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 questions
SynergisticMedia
 
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
Niit 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_17
Niit 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_04
Niit 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_16
Niit Care
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
Rohit Singh
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
Niit Care
 
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
devlabsalliance
 
Java interview question
Java interview questionJava interview question
Java interview question
varatharajanrajeswar
 
Best interview questions
Best interview questionsBest interview questions
Best interview questions
Thesis Scientist Private Limited
 
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
white paper
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
Ehtisham Ali
 
Design pattern application
Design pattern applicationDesign pattern application
Design pattern application
gayatri 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 miss
Mark Papis
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
Kuntal Bhowmick
 
Java
JavaJava
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
Arun 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_19
Niit 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_13
Niit Care
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
Niit Care
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
Niit Care
 
Dacj 1-1 c
Dacj 1-1 cDacj 1-1 c
Dacj 1-1 c
Niit Care
 
10 intel v_tune_session_14
10 intel  v_tune_session_1410 intel  v_tune_session_14
10 intel v_tune_session_14
Niit Care
 
NIIT Presentation 1
NIIT Presentation 1NIIT Presentation 1
NIIT Presentation 1
NIIT Bahrain
 
Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
Niit Care
 

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

Dacj 2-1 c
Dacj 2-1 cDacj 2-1 c
Dacj 2-1 c
Niit Care
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
 
Csharp
CsharpCsharp
Csharp
Swaraj Kumar
 
Dacj 2-2 c
Dacj 2-2 cDacj 2-2 c
Dacj 2-2 c
Niit Care
 
Aspdot
AspdotAspdot
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
Connex
 
VB Script Overview
VB Script OverviewVB Script Overview
VB Script Overview
Praveen Gorantla
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
Max Kleiner
 
Core java
Core javaCore java
Core java
Sun Technlogies
 
Core java
Core javaCore java
Core java
Shivaraj R
 
Gae icc fall2011
Gae icc fall2011Gae icc fall2011
Gae icc fall2011
Juan Gomez
 
Test design techniques
Test design techniquesTest design techniques
Test design techniques
Pragya Rastogi
 
Lecture1
Lecture1Lecture1
Lecture1
Amisha Dalal
 
Lesson 39
Lesson 39Lesson 39
Lesson 39
Avijit Kumar
 
AI Lesson 39
AI Lesson 39AI Lesson 39
AI Lesson 39
Assistant Professor
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
Niit Care
 
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,...
Blue Elephant Consulting
 
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...
Blue Elephant Consulting
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
Rohit Rao
 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
Hatem Abd El-Salam
 

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...
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
 

More from Niit Care

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

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

GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Neo4j
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
BibashShahi
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 

Recently uploaded (20)

GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 

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