Java - Storing Data (Part 2: Data Types)
Gaurav Gupta
Posted on September 12, 2021
Hey, there👋 Welcome back to Part 2 of Java Learnings Vault (If you haven't checked out Part 1, you can go right here). In this blog post, I'll be telling a bit about the various data types of Java.
Before starting with what are data types, we will first need what are variables.
What are Variables?
As we know, data is an important part of a program, basically the main ingredients of a program, and we need to store this data somewhere, right? We use variables to store this data.
The image above is an example of an integer variable (we'll discuss this in a moment) of the name "number" which stores the value 20.
What are Data Types?
As data need not always be an integer ie., a number, there are different types of data like a string (ex: HashNode), a decimal number (ex: 3.14), or simply a character (ex: z). For java to know what type is the data we are providing, we use "Data Types". So the above example can be simply given in the format:
A formal definition of data types is:
Data types specify the different sizes and values that can be stored in the variable.
Data types are broadly categorized into 2 categories:
- Primitive Data Types: The primitive data types include int, char, boolean, float, double, long, etc.
- Non - Primitive Data Types: The non-primitive data types include String, Arrays, etc.
In this blog post, we'll be discussing just the primitive data types.
Primitive Data Types
The primitive data types are the most basic in Java. Now, talking about different primitive data types.
Boolean: This data type is used to store only 2 possible values: true and false. This data type is usually used as flags to track simple true/false conditions. The Boolean data type specifies 1 bit of information.
Integer: This data type is used to store 32-bit signed whole numbers (signed numbers include both negative and positive values) between the range -2^31 to 2^31 - 1. The Integer data type takes 4 bytes of space.
Byte: The byte data type can store whole numbers from -128 to 127. This data type takes 1 byte of space (just like its name).
Short: The short data type can be used to store whole numbers from -32768 to 32767. This data type takes 2 bytes of space.
Long: The Long data type is used when we need to store values more than the range int can support. This data type takes 8 bytes of memory space.
You may have noticed in the image that the value is followed by an uppercase 'L'. This is done to explicitly state that the number is of long data type and not an int.
The ending need not be an uppercase 'L' but can even be a lowercase 'a, but it can often be mistaken as '1' (one) so uppercase is usually preferred.
Float: The float data type is used to store decimal numbers. Its value range is unlimited. The float data type takes 4 bytes of space. (source)
Note that we follow the value with an 'f' or 'F' to explicitly state it as a floating-point number.
Double: The double data type is also used to store decimal numbers. It also has an unlimited value range. (source) But it shouldn't be used to store precise values like currency. The double data type takes up 8 bytes of space.
Character: The char data type is used to store a single character. We surround the value of this data type with single quotes (' ').
Why so many data types to store whole numbers and decimals?🤔
After seeing these data types, you may be wondering why do we even need so many data types just to store a whole number? Why not just use long to store all whole numbers?
The reason is simple, if you look again I have mentioned that each data type takes a different amount of memory space. So, using the long data type to store all whole numbers results in the use of a larger amount of memory. To fully utilize memory we use different data types. The same reason applies to the use of float and double.
In simple terms, why would you want to use a box, the size of a fridge, just to store a small candy? So, we use appropriate-sized containers to store the items to efficiently manage space.
Summary
To sum up data types and their classification you can have a look at the below diagram.
To see the amount of memory size each data type takes, you can have a look at the table below.
This marks the end of this blog post. Any feedback and suggestions are greatly appreciated. If my posts are helping you, do comment and show some love by sharing them with your friends.💖
PS: You can also find me on HashNode
Bye, until next time👋
Posted on September 12, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.