We have completed writing, compiling and executing first Java programming. Now we need to dive into the language in more details. So next step should be to understand DataTypes in Java. Before that, we need to understand one basic concept, Variable.
What is 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 name and email of the users. User details will vary for each and individual person. So, when we write a programming, how we can store these details like name and email. That’s where we need 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 DataType?
Programming is about storing and manipulating data and each of the data has a type. Example, 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, DataTypes are categorized into two categories
- Primitive – System defined DataTypes that actually holds the values
- Non Primitive – Mostly user defined DataTypes, to hold reference of any object. Will dig more on this as we move forward
Here we are going to mostly talk about Primitive data types. In Java, we have 8 Primitive DataTypes
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 | 32-bit single precision floating point number | 6-7 decimal digits |
double | 64-bit double precision number | 15-16 decimal digits |
boolean | logical true or false values | true, false |
char | 16-bit unicode characters | Single character unicode value |
In the next article, when we will learn about Java literals, we will understand more on the examples on these Data Types.
What is Type conversion?
Type conversion means, changing onType to another Type. Although we cannot convert all the Types to one another, but we can convert between the numbers, number to character or character to numbers.