Java Magazine, May/June 2019
TYPE EFFECTIVE STORAGE RANGE OF VALUES boolean 1 BIT true AND false byte 1 BYTE 128 TO 127 short 2 BYTES 32768 TO 32767 char 2 BYTES 0 TO 65535 int 4 BYTES PLUS OR MINUS ABOUT 2 BILLION long 8 BYTES PLUS OR MINUS ABOUT 9 1018 float 4 BYTES PLUS OR MINUS ABOUT 34 1038 double 8 BYTES PLUS OR MINUS 18 10308 ORACLE COM JAVAMAGAZINE MAY JUNE 2019 82 fix this destination type the assignment from the expression to the destination type is permitted The converse also applies If an expression can represent values that are outside the range of the type of a destination such an assignment is rejected Lets look at the ranges of the primitive types Some things should be pretty clear boolean cannot be assigned to or from a numeric expression For the main integer numeric types assignment is possible from smaller to larger that is from byte to short to int to long For the floating point types assignment from float to double is possible Assignment from any of the integer types to either of the floating point types looks good also float and double do some magic to be able to store a vastly bigger range in the same amount of storage Perhaps a little surprising is the fact that assignment between short and char cannot be performed in either direction This is because a char can represent values greater than the maximum value of a short 65535 32767 and a short can represent negative values which are not representable by a char
You must have JavaScript enabled to view digital editions.