This time we'll get a brief introduction to data types in the Java language. We won't code yet, just get acquainted.
A Quick Look at Data Types
FYI, Java belongs to the strongly typed family of languages. That means the programmer must give every variable an explicit data type.
So what is a data type, and why does it have to exist?
A data type is an attribute attached to a piece of data. That attribute decides what treatments can be applied to the data.
For example, you have 2 pieces of data: someone's age and someone's job.
You know that an age always grows each time that person has a birthday, and it certainly can't be changed on a whim.
On the other side, someone's job can change around, but a job can't possibly grow every birthday (right?).
So these 2 pieces of data can be said to have different types. That's because the treatment you can apply to one piece of data can't always be applied to another.
Well, hope that gives you the picture.
Kinds of Data Types in Java
In general there are 2 kinds of data types in Java, primitive and constructed. Constructed types are usually called objects.
Let's take them one at a time.
Primitive Data Types
Don't let the word primitive send your mind to cavemen, programming uses the term too.
A primitive data type is a type that's there from the start. It's provided by the programming language, and even provided by the hardware you have. Most programming languages share the same primitives.
We may have touched on this when talking about variables in Java. But no harm going over it again so it doesn't slip away.
| Data Type | Description | Value | Size |
|---|---|---|---|
| boolean | Logical data type, true and false | true, false | 1 bit |
| byte | Raw data storage (characters, etc.) | -128 - 127 | 8 bit |
| char | Unicode character storage | unicode character | 16 bit |
| short | Short range whole number | -32768 - 32767 | 16 bit |
| int | Medium range whole number | to 2147483647 | 32 bit |
| long | Long range whole number | to | 64 bit |
| float | Decimal with 7 digit precision after the dot | 7 digit precision | 32 bit |
| double | Decimal with 16 digit precision after the dot | 16 digit precision | 64 bit |
From those primitive data types we can split them into a few groups:
- Logical: boolean
- Character: char
- Integer: byte, short, int, and long
- Float: float, and double
We'll dissect each one in a later piece.
Constructed Data Types (Objects)
Next come constructed data types. As the name suggests, the programmer builds these types, and they can be made of a combination of primitive data.
As an OOP language, everything in Java is an Object. An object is one kind of constructed data type. Inside an object you can add attributes as you need them.
Every primitive even has an object counterpart. To tell which is primitive and which is object, just look at the first letter. A capital first letter means object, and a lowercase one means primitive.
For example, the boolean data type has Boolean as its object counterpart. double has Double. Last, a slightly different one, int has Integer.
Naming objects with a capital first letter is a typing convention, a recommended writing rule in the Java language.
A constructed type you'll probably use often is String. It counts as constructed because it's a combination of several pieces of char data.
Besides objects, there are several kinds of constructed types you can create, among them:
- class: template for creating objects
- interface: template for creating classes
- enum: a data type with valid values set by the programmer
- array: a collection of data you can access by index
We'll cover class, interface, and enum further in the OOP in Java section.
Next Up
Now that we're acquainted, next we'll go deeper into each data type. We'll cover the rules, what operations you can do, and how to convert one type into another.
Hope it helps, happy coding 👋