-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeCasting.java
More file actions
45 lines (41 loc) · 1.4 KB
/
typeCasting.java
File metadata and controls
45 lines (41 loc) · 1.4 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
package BASICS;
@SuppressWarnings("unused")
public class typeCasting {
public static void main(String RGS[]){
int a=127;
byte b=100;
b=(byte)a; //EXPLICIT TYPE CASTING
System.out.println(b);
byte b_1=125;
a=b_1; //IMPLICIT TYPE CASTING
System.out.println(a);
float f=5.6f;
int x=(int)f;
System.out.println(x);
System.out.println("-----------------------");
int a1=12;
//byte k=a I can't do this
//therefore I will use typecasting
byte k=(byte) a1; //I've to use type casting
System.out.println(k);
//what if the integer value exceeds bytes range?,
//the type casting will perform a modulus operation,
//it will devide the int with bytes range i.e 256
//and return the REMAINDER.
//EG:
int i=300;
byte j=(byte)i;
System.out.println(j);
//TYPE PROMOTION
//when some ops are performed on two or more datatypes results in a value which
//exceeds the range of the defined datatype of the operands
//then JAVA promotes the result of a higher datatype(int).
//EG:
byte b1=10;
byte b3=40;
//let op be *, note b1*b3=400 which is way out of
//bytes range, so javac will convert it to higher datatype(int).
int res=b1*b3;
System.out.println(res);
}
}