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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -