SlideShare a Scribd company logo
1 of 13
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

More Related Content

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

9 cm604.8

  • 1. Type conversion and casting features 1
  • 2. Objectives On completion of this 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 of converting 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 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