Reputation: 2282
I am a little confused on when to use what primitives. If I am defining a number, how do I know what to use byte
, short
, int
, or long
? I know they are different bytes, but does that mean that I can only use one of them for a certain number?
So simply, my question is, when do I use each of the four primitives listed above?
Upvotes: 1
Views: 188
Reputation: 1108
@dasblinkenlight
When you are working for large project, you need to optimize the code for each requirement. Apparently you would reduce the memory occupied by the project.
Hence using int, short, long, byte plays a vital role in terms of optimising the memory. When the interger holds a 1 digit value then you declare the integer with int or short, but you can still choose long,float and double technically but to become a good programmer you can try to follow standards.
Byte - Its 8 bit. So it can hold a value from -128 to 127.
short: Its 32 bit. so -32,768 and a maximum value of 32,767
Long- Its 64 bit. -2 (power of) 63 and a maximum value of 2(power of)63-1 float and double- for large decimal values.
Boolean for Yes or No decesions- can hold a value of 1 or 0
Char for characters.
Hope this answer helps you.
Upvotes: 0
Reputation: 30245
The general guidance should definitely be: If you don't have any good reason to pick a specific primitive, pick int.
This has at least three advantages:
That said if you have an array of millions of elements yes you definitely should figure out what primitive fits the input range - I refer you to the other answers for a summary of how large each one is.
Upvotes: 2
Reputation: 42174
Do you know what values your variable will potentially hold?
If it will be between -128 and 127, use a byte
.
If it will be between -32,768 and 32,767, use a short
.
If it will be between -2^32 and 2^32 - 1, use an int
.
If it will be between -2^63 and 2^63 - 1, use a long
.
Generally an int
will be fine for most cases, but you might use a byte
or a short
if memory is a concern, or a long
if you need larger values.
Try writing a little test program. What happens when you put a value that's too large in one of these types of variables?
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
See also: the year 2038 problem
Upvotes: 3
Reputation: 727057
In order to use a primitive you need to know the data range of the number stored in it. This depends heavily on the thing that you are modeling:
byte
when your number is in the range [-128..127]short
when your number is in the range [-32768..32767]int
when your number is in the range [-231..231-1]long
when your number is in the range [-263..263-1]In addition, byte
data type is used for representing "raw" binary data, in which case it is not used like a number.
When you want an unlimited range, use BigInteger
. This flexibility comes at a cost: operations on BigInteger
can be orders of magnitude more expensive than identical operations on primitives.
Upvotes: 3
Reputation: 41281
If I am lets say defining a number, how do I know what to use byte, short, int, or long?
Depending on your use-case. There's no huge penalty to using an int
as opposed to a short
, unless you have billions of numbers. Simply consider the range a variable might use. In most cases it is reasonable to use int
, whose range is -2,147,483,648 to 2,147,483,647, while long
handles numbers in the range of +/- 9.22337204*1018. If you aren't sure, long
won't specifically hurt.
The only reasons you might want to use byte
specifically is if you are storing byte data such as parts of a file, or are doing something like network communication or serialization where the number of bytes is important. Remember that Java's bytes are also signed (-128 to 127). Same for short--might be useful to save 2GB of memory for a billion-element array, but not specifically useful for much other than, again, serialization with a specific byte alignment.
does that mean that I can only use one of them for a certain number?
No, you can use any that is large enough to handle that number. Of course, decimal values need a double
or a float
--double is usually ideal due to its higher precision and few drawbacks. Some libraries (e.g. 3D drawing) might use floats however. Remember that you can cast numeric types (e.g. (byte) someInt
or (float) functionReturningADouble();
Upvotes: 5
Reputation: 93
A byte can be a number from -128 to 127
A short can be a number from -32,768 to 32,767
An int can be from -2^32 to 2^32 - 1
A long is -2^63 to 2^63 - 1
Usually unless space is an issue an int is fine.
Upvotes: 4