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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: