java - Most efficient data type for an input of fixed numbers -


i have json coming java program. has particular field 6 fixed numbers: 0, 30, 60, 120, 240 or 480. possible in java choose better data type short? maybe using enum in form or representing input in bits taking advantage of knowing fixed input in advance?

regarding enums, seem made different use case, oracle java docs enum, looks if use enum, still end creating int internally, don't see advantage in speed or memory. there missing?

i tried google couldn't appropriate answer yet.

first, observe numbers example follow pattern - constructed powers of 2 multiplied 30:

  • 0 - 0 - 0*30
  • 1 - 20 - 1*30
  • 2 - 21 - 2*30
  • 4 - 22 - 4*30
  • 8 - 23 - 8*30
  • 16 - 24 - 16*30

if store small number between 0 , 5, inclusive, can compute target number either look-up table, or simple bit shifting expression:

byte b = ... // store value in variable of type "byte" int num = b!=0 ? 30*(1<<(b-1)): 0; 

note: since enum full-blown class, use or more space primitive number.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -