0

How is byte b accepting +256 when range of byte is -128 to +128.

class ByteShift { public static void main(String args[]) { byte a = 64, b; int i; i = a << 2; b = (byte) (a << 2); System.out.println("Original value of a: " + a); System.out.println("i and b: " + i + " " + b); } }

24th Feb 2017, 7:45 PM
rahat bhambri
rahat bhambri - avatar
4 Réponses
+ 5
Specialty of Java type cast - Java allows to store any 256 values as byte if you force it to. And you do by casting int 256 to byte.
24th Feb 2017, 8:39 PM
Tashi N
Tashi N - avatar
+ 5
No. 257 will overflow and output 1.
25th Feb 2017, 3:11 PM
Tashi N
Tashi N - avatar
0
The range of a byte has 256 possibilities. The range is -128 to 127. That's 128 negative numbers + 127 positive numbers + the number 0 = 256 possibilities.
24th Feb 2017, 7:51 PM
ChaoticDawg
ChaoticDawg - avatar
0
So , can't b store 257 ?
25th Feb 2017, 11:44 AM
rahat bhambri
rahat bhambri - avatar