Type conversion and casting features




                                       1
Objectives

On completion of this period, you would be able to
    know :


•   The type conversion and casting features




                     9CM604.8                    2
Recap

•   How to declare variables
•   How to initialize the variables




                                      3
Type Conversion

•   It is possible to assign a value of one type to a
    variable of different type without a cast
•   Java does the conversion of the assigned value
    automatically
•   This is known as ‘automatic type conversion’




                                                    4
Contd..
               Type Conversion

•   It is possible only if   the destination type has
    enough space to store the source value

                 eg. : byte m=9;

                      int k=18;




                                                         5
Contd..
                Type Conversion

•   The process of assigning a smaller type to a larger
    one is known as ‘widening’ or ‘promotion’
•   Assigning a larger type to a smaller one is known
    as ‘narrowing’
•   Narrowing may result in loss of information




                                                            6
Casting

•   To store a value of one type into a variable of
    another type, then we must cast
•   The value to be stored by preceding it with the
    type name in parentheses
•   The syntax is
          type variable1 = (type) variable2;
                    eg. : int d;
                          double m;
                          int d = (double) b;

                                                  7
Contd..
                       Casting
•   The process of converting one data type to another
    is called ‘casting’
•   Casting is necessary when a method returns a type
    different than we required
•   Four integer types can be cast to another type
    except boolean
•   Casting into smaller type may result in loss of data



                                                           8
Contd..
                      Casting

•   The float and double can be cast to any other type
    except boolean
•   Casting a floating point value to an integer value
    will result in loss of the fractional part




                                                       9
Summary

•   Automatic Type Conversion

•   Type Casting




                                10
Quiz

1.The process of converting one data type to another
    is called ______

a)   Casting
b)   Conversion
c)   Swapping
d)   All the Above


                                                   11
Frequently Asked Questions

•   How can you declare a Variable
•   Explain the initialization of a variable using read
    statement




                                                          12
swings
Struts
jdbc
hibernate
home
java previous question papers
 OCT/NOV-2012 QUESTION PAPER
April / May 2012 c-09
October/ November-2011 c-09
April/ May 2011 c-09      
April/ May 2011 c-05
                          

                                13

9 cm604.8

  • 1.
    Type conversion andcasting features 1
  • 2.
    Objectives On completion ofthis period, you would be able to know : • The type conversion and casting features 9CM604.8 2
  • 3.
    Recap • How to declare variables • How to initialize the variables 3
  • 4.
    Type Conversion • It is possible to assign a value of one type to a variable of different type without a cast • Java does the conversion of the assigned value automatically • This is known as ‘automatic type conversion’ 4
  • 5.
    Contd.. Type Conversion • It is possible only if the destination type has enough space to store the source value eg. : byte m=9; int k=18; 5
  • 6.
    Contd.. Type Conversion • The process of assigning a smaller type to a larger one is known as ‘widening’ or ‘promotion’ • Assigning a larger type to a smaller one is known as ‘narrowing’ • Narrowing may result in loss of information 6
  • 7.
    Casting • To store a value of one type into a variable of another type, then we must cast • The value to be stored by preceding it with the type name in parentheses • The syntax is type variable1 = (type) variable2; eg. : int d; double m; int d = (double) b; 7
  • 8.
    Contd.. Casting • The process of converting one data type to another is called ‘casting’ • Casting is necessary when a method returns a type different than we required • Four integer types can be cast to another type except boolean • Casting into smaller type may result in loss of data 8
  • 9.
    Contd.. Casting • The float and double can be cast to any other type except boolean • Casting a floating point value to an integer value will result in loss of the fractional part 9
  • 10.
    Summary • Automatic Type Conversion • Type Casting 10
  • 11.
    Quiz 1.The process ofconverting one data type to another is called ______ a) Casting b) Conversion c) Swapping d) All the Above 11
  • 12.
    Frequently Asked Questions • How can you declare a Variable • Explain the initialization of a variable using read statement 12
  • 13.
    swings Struts jdbc hibernate home java previous questionpapers  OCT/NOV-2012 QUESTION PAPER April / May 2012 c-09 October/ November-2011 c-09 April/ May 2011 c-09   April/ May 2011 c-05   13