Home > Fundamentals > Java Primitive Data Types – The Integrals

Java Primitive Data Types – The Integrals


I have recently encountered a need to know the difference between the different primitive data types that Java provides and I started to look around to find the answer – which was as expected quite a simple task, however the information I found was not laid-out the way I expected it to be, so here is Java Primitive Data Types (The Integrals) in a nutshell.

Type Size Range
byte 8 bit -128 to 127
Usage The byte data type is an 8-bit signed two’s complement integer.
The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable’s range is limited can serve as a form of documentation.
Type Size Range
short 16 bit -32,768 to 32,767
Usage The short data type is a 16-bit signed two’s complement integer.
As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
Type Size Range
int 32 bit -2,147,483,648 to 2,147,483,647
Usage The int data type is a 32-bit signed two’s complement integer.
For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.
Type Size Range
long 64 bit -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Usage The long data type is a 64-bit signed two’s complement integer.
Use this data type when you need a range of values wider than those provided by int
Type Size Range
boolean Undefined true/false
Usage The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its “size” isn’t something that’s precisely defined.
Type Size Range
char 16 bit ‘\u0000’ to ‘\uffff’ (or 0 – 65535)
Usage The char data type is a single 16-bit Unicode character.

Floating point numbers – will be covered in future posts.

More reading: Primitive Data Types (Java’s Tutorials)

Advertisements
Categories: Fundamentals Tags: , , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: