java - When to use short,byte and hex primitive types in program? -
short can accommodate value max 16 bit
but have not seen using shor primituve take. should not encourage when sure value fit in 32 bit. example :- see constant int value of 1 also. right?
byte
i not sure kind of scenario may requires use byte primitive data type byte var1 =3;
similarly not sure valid scenario wher developer has go hexadecimal values liken
public final static int var2 = 0x001d;
the short answer "never". longer answer almost never. primitives can hold int values smaller int
invented save memory. reason irrelevant applications. memory usage optimization (almost) passed away since computers became strong , memory cheap.
sometimes convenient use byte
type. due java not have unsigned
modifier (like c) byte can confusing: values greater 127 become negative. method read()
declared in inputstream returns int
instead of byte
.
sometimes memory usage optimization becomes relevant again. example in case of "big data" near real time applications have operate huge in-memory caches , store on disk terabytes of data.
Comments
Post a Comment