Use the eight primitive Java types

Java has 8 primitve data types.

Here is a list of them with their size and example use:

boolean – a single bit.
A bit is a single value in memory which can have 1 of 2 values – 0 or 1. Typically in programming, 0 means false and 1 means true but this relationship does not exist in java. A boolean has to be true or false, but this rule does not hold for the Boolean object.
Example of its use would be where an object has an ‘is’ or ‘has’ attribute, e.g. person.isDeceased(), person.isEligible();

short – a 16-bit number.
Of the 16 bits used, 1 of the bits is used to determine whether the value is positive or negative. This means the minimum value represented is -32768 and the maximum is 32767.
It is rarely used as an int is typically used for counting things and a Long is typically used for the Id field of an entity.

char – 16-bit character representation.
When used directly it represents a character. It can however be used as a number – it has MIN_VALUE and MAX_VALUE as do other number types, e.g. short, int, long. This does also mean it can be used with the normal mathematical operators such as +, -, % etc.

int – 32-bit integer numbers.
Always occupies 32 bits, regardless of processor, java version etc.
A typical usage would be counting the elements in a list or some returned results. Min value is -2147483648 and Max value is 2147483647. An int is often used even when the value expected would fit into a short. This is just force of habit I think.

float – 32-bit floating point.
This is used for calculating values with numbers after the decimal point, e.g. 10/3. Min value is 1.4E-45
and max value is 3.4028235E38. It shouldn’t be used for currency as it deals with approximations. Running the following:

System.out.println(1.03 - 0.42);

will output


long – 64-bit integer
This is typically used for very large numbers – the min is -9223372036854775808 and the max is 9223372036854775807. A long is typically used for the id field of an entity persisted in the database.

double – 64-bit floating point number.
I never use doubles, I never see doubles used and doubt I will see them used. I don’t do a lot of mathematical computation though – I typically write CRUD apps and have code around the workflow of CRUD, e.g. validation, converting to and from DTOs, persistence.

Hope this helps give an idea of the different types. I think the main concerns are whether you want to use whole numbers or not and if so the concern becomes how much space do you need to represent this number – generally the smaller the better but… shorts are rarely used even when appropriate.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s