Page 112 - computer science (868) class 11
P. 112

The important thing is that no part of the data is lost.
              As shown in the figure, we can easily understand the different patterns of implicit conversion. For example, a byte can
              be converted to short, which can further be converted to int, long, float, or double according to the requirements.
              Similarly, the char data type can be promoted to int, long, float, or double.
              When a char data type is converted to int, it generates the ASCII value of the character that is to be converted.
              For example, let us see the following programming snippet:
                 char ch = 'A';
                 int n = ch;
                 System.out.println(ch+ ": "+n);
              Output:
              A: 65          // here, 65 is the ASCII value of A

              Let us see an example,
                 class implicit_datatype
                 {
                     public static void main()
                     {
                          byte b = 12;
                          char c = 'A';
                          short s = 124;
                          int i = 5000;
                          float f = 34.6f;
                          double d = 12.12;
                          double output = (d * c) + (f - i)* (s * d);
                            System.out.println("Result of the above expression (d * c) + (f - i)* (s * d) =
                          "+output);
                     }
                 }
              The output of the preceding program is as follows:









              Since, double is the largest data type, the output we get is in double format.

              5.5.2  Explicit Type Conversion
              The concept of explicit data type conversion is used to assign a value of a larger data type to a smaller data type. This
              process is known as narrowing. It is the reverse state of implicit data conversion. It requires the user’s intervention.

              Before assigning the incompatible data type, we need to write down the data type within the brackets.
              Example 1:

                 double d = 4.56;
                 int n = (int)d;
                 System.out.println("Result :" + n);
              Output: Result : 4

              Example 2:
                 short i;


                110110  Touchpad Computer Science-XI
   107   108   109   110   111   112   113   114   115   116   117