Datatypes are an abstraction of sorts .. as far as computer is concerned there are no int
or short
. There is memory and there is data.
When you say int x
, you are saying to a computer "give me enough bytes to store an int", when you say short y
, you are saying ... you guessed it.
short
, as you would expect takes less bytes then an int
and therefore may (and often does) contain data in the adjacent bytes. When comparing data of different types the issue is "will adjacent bits cause skewed results or not?"
Whenever you compare two different datatypes you really are comparing bits stored in two different locations. Max number of individual bits stored to represent the data need to be the same size for a comparison to work
Casting is used to help with this.