0% found this document useful (0 votes)
8 views4 pages

Java Typecasting Examples and Outputs

The document provides examples of typecasting in Java, demonstrating how to convert various data types such as byte, int, char, short, float, long, and double. Each section includes code snippets and sample output for the conversions. The output illustrates the results of the typecasting operations, highlighting potential changes in value and representation.

Uploaded by

Gana reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Java Typecasting Examples and Outputs

The document provides examples of typecasting in Java, demonstrating how to convert various data types such as byte, int, char, short, float, long, and double. Each section includes code snippets and sample output for the conversions. The output illustrates the results of the typecasting operations, highlighting potential changes in value and representation.

Uploaded by

Gana reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Typecasting Examples with Output

Java Code:

public class TypeCastingOutput {


public static void main(String[] args) {

// 1. byte typecasting
byte b = 10;
[Link]("byte to short is: " + (short) b);
[Link]("byte to int is: " + (int) b);
[Link]("byte to char is: " + (char) b);
[Link]("byte to long is: " + (long) b);
[Link]("byte to float is: " + (float) b);
[Link]("byte to double is: " + (double) b);
[Link]("--------------------------------");

// 2. int typecasting
int i = 65;
[Link]("int to byte is: " + (byte) i);
[Link]("int to short is: " + (short) i);
[Link]("int to char is: " + (char) i);
[Link]("int to long is: " + (long) i);
[Link]("int to float is: " + (float) i);
[Link]("int to double is: " + (double) i);
[Link]("--------------------------------");

// 3. char typecasting
char c = 'A';
[Link]("char to byte is: " + (byte) c);
[Link]("char to short is: " + (short) c);
[Link]("char to int is: " + (int) c);
[Link]("char to long is: " + (long) c);
[Link]("char to float is: " + (float) c);
[Link]("char to double is: " + (double) c);
[Link]("--------------------------------");

// 4. short typecasting
short s = 120;
[Link]("short to byte is: " + (byte) s);
[Link]("short to int is: " + (int) s);
[Link]("short to char is: " + (char) s);
[Link]("short to long is: " + (long) s);
[Link]("short to float is: " + (float) s);
[Link]("short to double is: " + (double) s);
[Link]("--------------------------------");

// 5. float typecasting
float f = 123.45f;
[Link]("float to byte is: " + (byte) f);
[Link]("float to short is: " + (short) f);
[Link]("float to int is: " + (int) f);
[Link]("float to char is: " + (char) f);
[Link]("float to long is: " + (long) f);
[Link]("float to double is: " + (double) f);
[Link]("--------------------------------");

// 6. long typecasting
long l = 100L;
[Link]("long to byte is: " + (byte) l);
[Link]("long to short is: " + (short) l);
[Link]("long to int is: " + (int) l);
[Link]("long to char is: " + (char) l);
[Link]("long to float is: " + (float) l);
[Link]("long to double is: " + (double) l);
[Link]("--------------------------------");

// 7. double typecasting
double d = 456.78;
[Link]("double to byte is: " + (byte) d);
[Link]("double to short is: " + (short) d);
[Link]("double to int is: " + (int) d);
[Link]("double to char is: " + (char) d);
[Link]("double to long is: " + (long) d);
[Link]("double to float is: " + (float) d);
}
}

Sample Output:

byte to short is: 10


byte to int is: 10
byte to char is:
byte to long is: 10
byte to float is: 10.0
byte to double is: 10.0
--------------------------------
int to byte is: 65
int to short is: 65
int to char is: A
int to long is: 65
int to float is: 65.0
int to double is: 65.0
--------------------------------
char to byte is: 65
char to short is: 65
char to int is: 65
char to long is: 65
char to float is: 65.0
char to double is: 65.0
--------------------------------
short to byte is: 120
short to int is: 120
short to char is: x
short to long is: 120
short to float is: 120.0
short to double is: 120.0
--------------------------------
float to byte is: 123
float to short is: 123
float to int is: 123
float to char is: {
float to long is: 123
float to double is: 123.44999694824219
--------------------------------
long to byte is: 100
long to short is: 100
long to int is: 100
long to char is: d
long to float is: 100.0
long to double is: 100.0
--------------------------------
double to byte is: -56
double to short is: 456
double to int is: 456
double to char is: Lj
double to long is: 456
double to float is: 456.78

You might also like