HashMap internally has its own static final variables for its working.
static final int DEFAULT_INITIAL_CAPACITY = 16;
Why can’t they use byte datatype instead of using int since the value is too small.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using
byteorshortfor variables and constants instead ofintis a premature optimization that has next to no effect.Most arithmetic and logical instructions of the JVM work only with
int,long,floatanddouble, other data types have to be cast to (usually)ints in order for these instructions to be executed on them.The default type of number literals is
intfor integral anddoublefor floating point numbers. Usingbyte,shortandfloattypes can thus cause some subtle programming bugs and generally worsens code readability.A little example from the Java Puzzlers book:
This program doesn’t print
Joy!, because the hex value0x90is implicitly promoted to anintwith the value144. Sincebytes in Java are signed (which itself is very inconvenient), the variablebis never assigned to this value (Byte.MAX_VALUE = 127) and therefore, the condition is never satisfied.All in all, the reduction of the memory footprint is simply too small (next to none) to justify such micro-optimisation. Generally, explicit numeric types of different size are not necessary and suitable for higher level programming. I personally think that only case where smaller numeric types are acceptable are byte arrays.