Doubt in Unboxing error
Got a code from Complete Reference
public static void main(String s)
The output is -24. I am not clear about how come we are getting -24? In the book it written that we are getting garbage value. But there should be some reason of getting this value -24.
1000 is greater than byte's maximum value of 127, and so you're getting an expected result.
For those who like bits and bytes:
Notice the rightmost two nibbles(1 byte): e8
int ix = 1000;
System.out.println("1000=" + Integer.toHexString(ix)); // 1000=3e8
ix = -24;
System.out.println("-24=" + Integer.toHexString(ix)); //-24=ffffffe8
Think of it as if it wraps, and when a value goes over, it goes to the minimum value. 128 is greater than a bytes maximum value, so it 'wraps around' to -126. 1000 'wraps around' multiple times, and ends up at -24. It's not exactly what happens, and Norm's example is more complete, but it's an easy way to think of it when you're trying to figure out what happens when a numeric value goes over the maximum of its type.