What Primitive To Use In Java

195 views Asked by At

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?

6

There are 6 answers

1
nanofarad On BEST ANSWER

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();

0
jknam On

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.

0
Sergey Kalinichenko On

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:

  • Use byte when your number is in the range [-128..127]
  • Use short when your number is in the range [-32768..32767]
  • Use int when your number is in the range [-231..231-1]
  • Use 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.

0
Kevin Workman On

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

0
Voo On

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:

  1. Every kind of arithmetic or logical operation will return an int. Hence if you use anything smaller you will have to cast a lot to actually assign values back.
  2. It's the accepted standard for "some number". If you pick anything else, other developers will wonder about the reasons.
  3. It will be the most efficient, although it's likely that the performance difference from using anything else will be minuscule.

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.

0
A user On

@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.