Question

short can accommodate the value max 16 bit

But i have not seen anybody using shor primituve take. Should it not be encourage when i am sure value will fit with in 32 bit. For example :- i generally see constant as int for value of 1 also. Is it right?

byte

I am not sure what kind of scenario i may be requires to use byte primitive data type like byte var1 =3;

Similarly not sure what are the valid scenario wher developer has to go for hexadecimal values liken

   public final static int    var2   = 0x001D;
Was it helpful?

Solution

The short answer is "never". The longer answer is almost never. Primitives that can hold int values but are smaller than int are invented to save memory. This reason is irrelevant for most applications. Memory usage optimization (almost) passed away since computers became very strong and memory very cheap.

Sometimes it is convenient to use byte type. However due to java does not have unsigned modifier (like C) byte can be confusing: you values greater than 127 become negative. Even method read() declared in InputStream returns int instead of byte.

Sometimes however memory usage optimization becomes relevant again. For example in case of "big data" near real time applications that have to operate with huge in-memory caches and store on disk terabytes of data.

OTHER TIPS

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

usually short / byte is used when saving memory is crucial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top