Java Data Types: Understanding the 8 Primitive Data Types in Java
Classifying data into different data types (or more often, simply ‘types’) is a fundamental preoccupation of programming. Most programming languages categorize data into strict categories. This is particularly true for strongly typed languages (i.e. languages which do not permit data from one type to be used in another type) like Java.
You can learn more about data types in Java in this introduction to Java programming.
In this blog post, we will learn about the 8 data types in Java and their characteristics.
Data Types in Java
As mentioned above, Java is a strongly typed language. Although there are no universally agreed definitions of what ‘strongly typed’ actually means, general consensus holds that a ‘strongly typed’ language does not permit data from one declared variable type to be used in another. The implication, in layman speak, is that you have to explicitly declare what type of data a variable belongs to before declaring it.
You undertake such “strongly typed” declarations in real life all the time. When you say, “John is a man”, you essentially declare that: a) John is male, and b) John is old enough to not be a boy or child. Henceforth, for all practical purposes, John will remain classified as a ‘man’ and cannot be used in other similar categories (like ‘woman’, ‘boy’, etc.).
It’s the same case with data types in languages like Java. Once you say that a variable is an integer, it cannot be used to store decimal values or strings.
Want to make Android apps? Learn about Java for Android in this course.
Java Primitive Data Types
Any data type built-into a programming language is called ‘primitive’ data type (the word itself betrays its meaning). Built-in data types are the basic building blocks of a programming language. It is often possible to combine them to create composite data types. The basic behavior of a primitive data type itself, however, cannot be modified (it is, after all, something primitive to the language – like the human Id).
There are eight primitive data types in Java. These are as follows:
1. Byte: A byte, for those of you who skipped CS 101, is one of the most basic units of memory made up of 8 individual bits. Byte data types in Java have the following characteristics:
Minimum Value: -128 (2^7)
Maximum Value: 127 (2^7-1)
Default Value: 0
byte x = 56
byte y = 68
Thus, you can save numbers between -128 and 127 (inclusive) in a byte. Bytes, because of their size, are useful for storing small data in large arrays. Another programmer looking through your code will also instantly recognize that a byte type will hold only a small value, thus improving your code’s readability (a major issue for large applications).
If this seems too complicated, try this course to learn more about Java fundamentals.
2. Short: A short is twice the size of a byte, i.e. it is made up of 16-bits. Its chief characteristics are:
Minimum Value: -32,768 (2^15)
Maximum Value: 32,767 (2^15-1)
Default Value: 0
short x = 9870
short y = -635
Like bytes, short types are useful alternatives to int (see below) data types, particularly if your data falls within the specified range. As with byte, using short also improves code readability, besides saving memory.
3. Int: An integer is four times the size of a byte (i.e. it is made up of 32 bits). It is one of the most commonly used data types in Java.
Minimum Value: -2,147,483,648 (2^31)
Maximum Value: 2,147,483,647 (2^31 – 1)
Default Value: 0
int x = 150000
int y = -2004320
As the most easily understood data type, you will use int a lot in your code.
4. Long: A long data type is twice the size of an integer, i.e. it is made up of 64-bits. It’s chief characteristics are:
Minimum Value: -9,223,372,036,854,775,808 (2^63)
Maximum Value: 9,223,372,036,854,775,807 (2^63 – 1)
Default Value: 0
long x = 6778005876543
long y = -554233254242
You’ll use long only if you encounter data that doesn’t fit within the int range (which will be rare).
5. Float: In programming, any decimal or fractional value is called a ‘float’. If there is a decimal after the number, it will be classified as a float. In Java, a float is made up of 32-bits IEEE floating points*.
The minimum/maximum value of float is not the same as that of the int data type (despite both being made of 32-bits). The full range of float values is beyond the scope of this tutorial. For now, the only thing you need to know is that you’ll use float (and double – see below) for saving decimal values.
float x = 2.321
float y = 1.234
*The float value range depends on the IEEE standard classification for floating point numbers. You can read about it here.
6. Double: Double is a data type that is twice the size of a float. I.e. it is made up of 64-bit IEEE floating points.
As with float, discussing the minimum/maximum value of double data type is beyond the scope of this article. What you should know is that double is a much more precise type than float. For all practical purposes, it is recommended that you use double instead of float for storing decimal values.
double a = 1.245240
double y = 12.2232
7. Char: Char data type refers to a single 16-bit Unicode character. Unicode is a computer industry standard for representing text related data. This includes alphabets, symbols ($, &, *, #, @, !, etc.), and special figures such as ¢, £, ¥, etc. The Unicode character set includes over 110,000 characters covering more than 100 language scripts.
In other words, any data besides numbers goes into the char data type.
char name = ‘John’
char country = ‘USA’
8. Boolean: Boolean is the smallest data type in Java, i.e. it is made up of only one bit. Thus, a Boolean data type can have only two values – 0 (or False) and 1 (or True).
boolean x = true
boolean y = false
(Tip: ‘True’ and ‘False’ written above are not strings. Do not enclose them within quotes as we did with the char example above)
If you found these data types a little confusing, taking a comprehensive Java foundation course such as would be of great help.
Tips, questions, opinions? We’d love to hear them! Share them with us in the comments below.
Top courses in Java
Java students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.