Data types are an abstraction of types. As far as the computer is concerned, there is no int or short . There is memory and there is data.
When you say int x , you tell the computer "give me enough bytes to store int", when you say short y , you say ... you guessed it.
short , as you expected, takes fewer bytes, and then int and, therefore, can (and often) contain data in neighboring bytes. When comparing data of different types, the problem is that "adjacent bits cause distorted results or not?"
Whenever you compare two different types of data, you really compare bits stored in two different places. The maximum number of individual bits stored to represent data must be the same size for comparison to work
Casting is used for this.
source share