Write a program to demonstrate typecasting in java.
Program Algorithm:
Step 1: create a class containing the main function
Step 2: declare an integer, byte, double variable.
Step 3: convert the integer to the byte using (byte).
Step 4: convert double to integer using (int).
Step 5: convert double to the byte using (byte).
Program Code:
class Coversion
{
public static void main(String args[])
{
byte b;
int i=255;
double d=323.142;
System.out.println("\nConversion of int into byte:-");
b=(byte)i;
System.out.println("i and b:"+i+" and "+b);
System.out.println("\nConversion of double to int:-");
i=(int)d;
System.out.println("d and i:"+d+" and "+i);
System.out.println("\nConversion of double to byte:-");
b=(byte)d;
System.out.println("d and b:"+d+" and "+b);
}
}
Sample Inputs and Outputs:
Conversion of int into byte:-
i and b:255 and -1
Conversion of double to int:-
d and i:323.142 and 323
Conversion of double to byte:-
d and b:323.142 and 67
Program Algorithm:
Step 1: create a class containing the main function
Step 2: declare an integer, byte, double variable.
Step 3: convert the integer to the byte using (byte).
Step 4: convert double to integer using (int).
Step 5: convert double to the byte using (byte).
Program Code:
class Coversion
{
public static void main(String args[])
{
byte b;
int i=255;
double d=323.142;
System.out.println("\nConversion of int into byte:-");
b=(byte)i;
System.out.println("i and b:"+i+" and "+b);
System.out.println("\nConversion of double to int:-");
i=(int)d;
System.out.println("d and i:"+d+" and "+i);
System.out.println("\nConversion of double to byte:-");
b=(byte)d;
System.out.println("d and b:"+d+" and "+b);
}
}
Sample Inputs and Outputs:
Conversion of int into byte:-
i and b:255 and -1
Conversion of double to int:-
d and i:323.142 and 323
Conversion of double to byte:-
d and b:323.142 and 67
Post A Comment:
0 comments: