We have completed writing, compiling, and executing our first Java program. Now we need to dive into the language in more detail. So, the next step should be to understand Data Types in Java. Before that, we need to understand one basic concept: a Variable.
What is a variable?
It’s not specific to any programming language. I would rather say it’s a mathematical concept. Let’s say you are writing an app that needs to store the names and emails of the users. User details will vary for each individual person. So, when we write a program, how can we store these details like name and email? That’s where we need a variable. In programming, a variable is a placeholder to store any value, and during execution, some memory is allocated to the variable to store the value.
What is a Data Type?
Programming is about storing and manipulating data, and each piece of the data has a type. For example, the name should be combination of alphabets like “John” or “Amit” , age should be integer number like “20” or “30” etc. In Java, when we need to store the values into a variable, we need to declare the variable, mentioning the type of data it’s going to hold. Before discussing how we need to declare the variables in Java using DataTypes, let’s understand what are different data types we have.
In Java, Data Types are categorized into two categories
- Primitive – System-defined Data Types that actually hold the values
- Non Primitive – Mostly user-defined Data Types, to hold a reference to any object. We will learn more bout Non-Primitive Data Types in detail.
Here we are going to mostly talk about Primitive data types. In Java, we have 8 Primitive Data Types
Data Type | Details | Value Range |
byte | 8-bit signed integer number | (-2⁷) to (2⁷ – 1) -128 to 127 |
short | 16-bit signed integer number | (-2¹⁵) to (2¹⁵ – 1) |
int | 32-bit signed integer number | (-2³¹) to (2³¹ – 1) |
long | 64-bit signed integer number | (-2⁶³) to (2⁶³ – 1) |
float | 16-bit Unicode characters | 6-7 decimal digits |
double | 64-bit double precision number | 15-16 decimal digits |
boolean | logical true or false values | true, false |
char | Single-character Unicode value | Single-character Unicode value |
In the next article, we will learn about Java literals, and we will understand more about the examples of these Data Types.
So far, we have explored Primitive Data Types. We are yet to learn about Non-Primitive Data Types and one critical aspect of Data Types, i.e., Type Conversion. Converting data from one DataType to another and how that works in Java.