Casting: Explicit Conversion

  1. User can require/force Java to convert from a "wider" type to a "narrower" type

    An example of such a conversion is from a int value to a short value to

  2. Casting: forced/explicit conversion.

    The conversion is be forced to take place, even though it may cause significant error

  3. How to request casting:
    (type) expression
    The value of expression is forcibly converted into the type type

    	int i;
    	float j;
    	i = (int) j;

  4. Example Java program showing that casting sometimes produces major errors: click here DEMO

    Moral: use casting only if you are sure that the value is within the range of the type to which you are converting !!!