If you only use it to express status, there is no difference between short and int. If you don’t believe me, you can compile it and look at the bytecode.
Code
int flag = 1; short flag = 1;
and code
int flag = 1; int flag = 1;
The resulting bytecode isexactly the same! You will get the following bytecode,
Depending on your usage scenario, if it is used for object attributes or SQL parameters, it is best to use Integer. Because you may not initialize it, using int will have a default value of 0 (this 0 may not be what you want)
If you only use it to express status, there is no difference between short and int. If you don’t believe me, you can compile it and look at the bytecode.
Code
and code
The resulting bytecode isexactly the same! You will get the following bytecode,
Depending on your usage scenario, if it is used for object attributes or SQL parameters, it is best to use Integer. Because you may not initialize it, using int will have a default value of 0 (this 0 may not be what you want)
If the status only has 0 and 1, you can use
boolean
(true
或false
) 或者byte
(0 或 1,byte
的范围在 -128 ~ 127);状态如果较多,更推荐用enum
Both boolean and int are available. There is no difference.