There are two thing you need to understand
1) the '~' inverts all the bits. Since a '4' is represented as
00000000 00000000 00000000 00000100
when you invert the bits you get
11111111 11111111 11111111 11111011
2) now you need to understand how
java interprets this value. google or search this forum for "two's complement". basically, you look a the left-most digit, and if it's a 1, you know your value will be negative. you then invert all the digits and add one, so you get
which is 5. and we know our answer is negative, so "11111111 11111111 11111111 11111011" = -5.