forked from BarkaBoss/Java-Full-Stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeCasting.java
More file actions
25 lines (21 loc) · 731 Bytes
/
Copy pathTypeCasting.java
File metadata and controls
25 lines (21 loc) · 731 Bytes
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
package DatatypesAndVariables;
public class TypeCasting {
public static void main(String[] args) {
//Implicit typecasting -> widening
short x = 5;
int y = x;
System.out.println(y);
//Explicit typecasting -> narrowing
int v = 5;
short w = (short) v;
System.out.println(w);
//Lost in precision in explicit typecasting
int num = 50000;
short numX = (short) num;//Short can take the current value of num
System.out.println(numX);
//Loss of floating point precision
float num1 = 67.87f;
Long num2 = (long) num1;
System.out.println(num2);//.87 will be lost since Long only takes whole numbers
}
}