In this tutorial, you’ll learn about Data types in Java, Primitive data types, Non-primitive data types and more.
In computer science, data is information that is stored or processed by a computer.
There are many data points we use in everyday life. Your name, your age, the number of apples in your pantry, whether your kitchen light is on or off. These can all be considered pieces of data, and we represent pieces of data in code using data types. Similar to other programming languages, Java classifies different pieces of data with data types based on their value.
For example, there’s a data type for letters and symbols, and there are various data types for numbers.
What is Data Types in Java
A data type provides a set of possible values, and if a piece of data is one of these values, it is classified as that specific type. Java separates its data types into two main categories that are
- Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float, and double.
- Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
Let’s deeper into these primitive types,
• Integers This group includes byte, short, int, and long, which are for whole-valued signed numbers.
• Floating-point numbers This group includes float and double, which represent numbers with fractional precision.
• Characters This group includes char, which represents symbols in a character set, like letters and numbers.
• Boolean This group includes boolean, which is a special type for representing true/false values.
Let’s have a look at datatype’s default value and default size.
Data Type | Default Value | Default size |
---|---|---|
boolean | false | 1 bit |
char | ‘\u0000’ | 2 byte |
byte | 0 | 1 byte |
short | 0 | 2 byte |
int | 0 | 4 byte |
long | 0L | 8 byte |
float | 0.0f | 4 byte |
double | 0.0d | 8 byte |