Type conversion is very much necessary in the world of programming. We all know when it is needed. There are three types of type conversion in Java.
The following type conversion is possible from one datatype to another datatype.
The following type conversion is possible from one datatype to another datatype.
- Widening
- Narrowing
- Mixed
- Widening Conversion
The following type conversion is possible from one datatype to another datatype.
- byte to short, int, long, float, double
- short to int, long, float, double
- int to long, float, double
- long to float, double
- float to double
int i = 123456789; float f = i; // Widening conversion int j = (int) f; // Narrowing conversion
- Narrowing Conversion
The following type conversion is possible from one datatype to another datatype.
- double to float, long, int, short, byte
- float to long, int, short, byte
- long to int, short, byte
- int to short, byte
- short to byte
int i = 12345; short s = (short) i;
- Mixed Conversion
The following type conversion is possible from one datatype to another datatype.
- byte to char
- short to char
- char to short, byte
char c= 88; //X System.out.println( c = (char)(c+2)); // will print Z
No comments:
Post a Comment