Java: Why can’t I cast int to Long

I think the question was not about casting primitives and wrappers in general. The question was about difference between casting int to java.lang.Long and int to java.lang.Short for example. JLS: “In addition, if the expression is a constant expression (§15.28) of type byte, short, char or int: A narrowing primitive conversion may be used if … Read more

Overflowing Short in java

What’s happening is that your number is wrapping around. More specifically, you’ve got a number 30,000, which in binary is: 0111 0101 0011 0000 When you add it to itself, and carry the 1’s, you get: 1110 1010 0110 0000 (Note: it’s very easy to multiply a number by 2 in binary — just shift … Read more