What Primitive To Use In Java -


i little confused on when use primitives. if defining number, how know use byte, short, int, or long? know different bytes, mean can use 1 of them number?

so simply, question is, when use each of 4 primitives listed above?

if lets defining number, how know use byte, short, int, or long?

depending on use-case. there's no huge penalty using int opposed short, unless have billions of numbers. consider range variable might use. in cases reasonable use int, range -2,147,483,648 2,147,483,647, while long handles numbers in range of +/- 9.22337204*1018. if aren't sure, long won't hurt.

the reasons might want use byte if storing byte data such parts of file, or doing network communication or serialization number of bytes important. remember java's bytes signed (-128 127). same short--might useful save 2gb of memory billion-element array, not useful other than, again, serialization specific byte alignment.

does mean can use 1 of them number?

no, can use large enough handle number. of course, decimal values need double or float--double ideal due higher precision , few drawbacks. libraries (e.g. 3d drawing) might use floats however. remember can cast numeric types (e.g. (byte) someint or (float) functionreturningadouble();


Comments

Popular posts from this blog

symfony - TEST environment only: The database schema is not in sync with the current mapping file -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -