Repository navigation
Expand file tree
/
Copy pathtypeCasting.java
More file actions
48 lines (41 loc) · 1.44 KB
/
Copy pathtypeCasting.java
File metadata and controls
48 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class typeCasting{
public static void main(String args[]) {
/* Widening(Automatic Conversion):
-Happens when smaller data types are converted into a larger data type.
-Safe conversion because no data lost.
-Performed automatically by the compiler.
*/
byte b = 127;
short s = b;
int i = s;
long l = i;
float f = l;
double d = f;
System.out.println("Byte : " + b);
System.out.println("Short : " + s);
System.out.println("Integer: "+i);
System.out.println("Long : "+l);
System.out.println("Float : "+f);
System.out.println("Double :"+d);
System.out.println("-------------------------------------");
/* Narrowing(Explicit Conversion):
-Happens when larger data type is converted into a smaller data types.
-Not Safe because precision or data may lost.
-Require explicit cast operator.
*/
double D = 123.456;
float F = (float)D;
long L = (long)F;
int I = (int)L;
short S = (short)I;
byte B = (byte)S;
char C = (char)B;
System.out.println("Double :"+D);
System.out.println("Float : "+F);
System.out.println("Long : "+L);
System.out.println("Integer: "+I);
System.out.println("Short : " + S);
System.out.println("Byte : " + B);
System.out.println("Char :" + C);
}
}