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

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

c# - Search and Add Comment with OpenXML for Word -