Friday, 18 January 2013


Program to Demonstrate Typecasting in Java (BlueJ)

Code:
/***** PROGRAM TO DEMONSTRATE TYPECASTING *****/
public class TypeCast
{
    public static void main()
    {
        System.out.println("***** Type Cast Demo *****");
        int i = 10;
        float f = 10.10f;
        double d = 12.12;
        char c = 'a';
        boolean b = true;
       
        int res1 = (int) f; // EXPLICIT TYPE CASTING
        System.out.println("type cast float 10.10 to int ==> "+res1);
        System.out.println("Type cast char "+ c + " to int ==> "+(int)c);
        double resd = i+f; // IMPLICIT TYPE CASTING
        System.out.println("type cast addition of "+i+ " + " + f + " = "+resd);
       
     
    }
}

OUTPUT:
***** Type Cast Demo *****
type cast float 10.10 to int ==> 10
Type cast char a to int ==> 97
type cast addition of 10 + 10.1 = 20.100000381469727

Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment