-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeCasting.java
92 lines (77 loc) · 3.09 KB
/
TypeCasting.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public class TypeCasting {
public static void main(String[] args)
{
//manual casting...
int integer = 128;
byte byt = (byte) integer;
/*
binary representation of 128 is :
24 0s + 1000000
when we try to fit it in byte (stores 127 max), it'll change
its first (sign) bit, and make it (-ve) by discording first 24 0s
*/
System.out.println("value stored in byt is : " + byt);
double dbl = 7.77;
int inT = (int) dbl;
System.out.println("value stored in inT is : " + inT);
//using left-shift <<
byte lShift = 2;
lShift = (byte) (lShift << 1);
/*
binary representation of 2 is : 00000010
after shifting to left by 1 digit, it'll be 00000100; and 10(binary) == 2 (decimal)
therefore, it'll print 2, a decimal number.
/*
again,
a = (byte) (a << 2); will give us :
2 digit shifting from real position.
00000010 == 2 (val of a, primarily)
00001000 == 8 (val of a, currently)
*/
System.out.println("after left shifting : " + lShift);
//using right-shift.
byte rShift = 2;
rShift = (byte) (rShift >> 1); //1 is the number of digit i want to shift right to.
/*
binary representation of 2 is : 00000010
after shifting to right by 1 digit, it'll be 00000001; and 1 (binary) == 1 (decimal)
therefore, it'll print 1, a decimal number.
/*
again,
a = (byte) (a >> 2); will give us :
2 digit shifting from real position.
00000010 == 2 (val of a, primarily)
00000000 == 0 (val of a, currently)
*/
System.out.println("after right shifting : " + rShift);
//condition checking; returns either true or false
int x = 5;
int y = 7;
System.out.println(x>y);
// while(0) & while(1)
System.out.println("true");
short srt = 128;
byte bt = (byte) srt;
System.out.println("128 in byte is : " + bt); // prints -128
/*
binary representation of short s = 0000000010000000 since short has 16 bits.
when we cast it to 'byte', it discards its first 8 bits (because 'byte' can hold only 8 bits),
and it lefts out '10000000'. First bit is sign bit, so we'll have a negative value.
so, decimal of it will be :
-1*2^7 + 0*2^6 + 0*2^5 + .... + 0*2^0 = -128 (OUTPUT)
*/
short a = 255;
byte b = (byte) a;
System.out.println("225 in byte is : " + b); // prints -1
/*
binary representation of short b = 0000000011111111 since short has 16 bits.
when we cast it to 'byte', it discards its first 8 bits (because 'byte' can hold only 8 bits),
and it lefts out '11111111'. First bit is sign bit, so we'll have a negative value.
so, decimal of it will be :
-1*2^7 + 1*2^6 + 1*2^5 + 1*2^4 + .... + 1*2^0 = -1 (OUTPUT)
*/
byte f = 5;
byte g = (byte) (f + 5);
System.out.println(g);
}
}