Archangel Macsika Sikademy Image
Archangel Macsika

Ultimate Tutorial on Data types in Java Programming

Tutorial 6 of 9 | 9 minutes read
Ultimate Tutorial on Data types in Java Programming - Sikademy

In java, identifiers like variables are defined by indicating the type of data they are permitted to store. This is required by the operating system in order to allocate memory space to that identifier and also decide the limit of possible and acceptable value.

Therefore, different data types can be assigned to identifiers like variables, which in turn can store values like integers, decimals, or characters etc.

Classification of Data Types in Java

In Java, data types are classified into two groups:

Primitive Data Types includes byte, short, int, long, float, double, boolean and char.

Non-Primitive Data Types also called Reference/Object Data Types includes String, Arrays and Classes.

Note: The String data type is so commonly used in Java, that some people mistake it for a primitive data type. However, it is actually a non-primitive data type, because it refers to an object.

Difference between primitive and non-primitive data types in Java

Primitive Data typeNon-primitive Data type
Primitive types are predefined (already defined) in Java.Non-primitive types are created by the programmer and is not defined by Java (except for String).
Primitive types cannot be used to call methods to perform certain operations.Non-primitive types can be used to call methods to perform certain operations.
A primitive type always have a value, and cannot be null.Non-primitive types can be null.
A primitive type starts with a lowercase letter.Non-primitive types starts with an uppercase letter.
The size of a primitive type depends on the data type.All Non-primitive types have the same size.

Primitive Data Types in Java

A primitive data type defines the size and type of values that an identifier holds. Primitive data types are predefined in Java without additional methods and indicated using a keyword.

There are eight primitive data types supported in Java programming language as listed below:

Data TypeSizeDescription
byte1 byteStores whole numbers from -128 to 127
short2 bytesStores whole numbers from -32,768 to 32,767
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
long8 bytesStores whole numbers from -9,223,372,036,854,775.808 to 9,223.372,036,854,775,808
float4 bytesStores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits
double8 bytesStores fractional numbers from 1.7e−308 to 1.7e+038. Sufficient for storing 15 decimal digits.
boolean1 byteStores true or false values
char2 bytesStores a single character/letter

Let's take a look at each. The eight primitive data type above can be further classified into their related variance. integer numbers, floating-point numbers (decimal numbers), characters, and boolean.

Integer Numbers in Java Primitive Data type

Integer data types can store positive or negative whole numbers such as 123 or -456, without any decimal point. The type you should use on an identifier depends on the numeric value assigned to that identifier.

That being said, the valid integer types are byte, short, int and long. The int type is the most commonly used data type.

Types of Integer Numbers in Java

Byte written as byte

Byte data type is an 8-bit signed two's complement integer that can store whole numbers from -128 (-27) as the minimum value to 127 (27 - 1) as the maximum value, and 0 (zero) as the default value.

It can be used in place of int or other integer types to save memory space especially in large arrays when you are certain that the value will be within -128 and 127.

Save, compile and execute the program.

Result:

80

Short written as short

Short data type is a 16-bit signed two's complement integer that can store whole numbers from -32768 (-215) as the minimum value to 32767 (215 - 1) as the maximum value, and 0 (zero) as the default value.

It can also be used in place of int to save memory space when the value is within -32768 and 32767.

Save, compile, and execute the program.

Result:

8000

Int written as int

Int data type is a 32-bit signed two's complement integer that can store whole numbers from -2147483648 (-231) as the minimum value to 2147483647 (231 - 1) as the maximum value, and 0 (zero) as the default value.

Generally, the int data type is the preferred data type when creating identifiers like variables with an integer value unless there is a concern about memory. You will encounter it a lot in this tutorial series.

Save, compile, and execute the program.

Result:

328087772

Long written as long

Long data type is a 64-bit signed two's complement integer that can store whole numbers from -9223372036854775808 (-263) as the minimum value to 9223372036854775808 (-263 - 1) as the maximum value, and 0L as the default value.

The long data type should only be used when you are storing a whole number longer than the int range.

Note: Remember to add "L" at the end of the value, It is mandatory, otherwise, you will get an error

Save, compile, and execute the program.

Result:

19390000000000

Floating Point Numbers in Java Primitive Data type

Floating point data types indicate numbers containing a decimal point such as 0.123 or 456.789. The two valid floating point types are float and double.

Types of Floating Point Numbers in Java

Float written as float

Float data type is a single-precision 32-bit IEEE 754 floating point that can store fractional numbers from 3.4e−038 to 3.4e+038, and 0.0f as the default value.

The float data type is mainly used to save memory in large arrays of floating point numbers and not recommended to be used for precise values such as currency.

Note: Remember to add "f" at the end of the value, It is mandatory, otherwise, the Java compiler will treat it as a double data type.

Save, compile, and execute the program.

Result:

204.53

Double written as double

Double data type is a double-precision 64-bit IEEE 754 floating point that can store fractional numbers from 1.7e−308 to 1.7e+038, and 0.0d as the default value.

Generally, the double data type is the preferred data type when creating identifiers like variables with a floating point value. You will encounter it a lot in this tutorial series.

Note: For double, adding the "d" is is optional because a decimal without an 'f' or a 'd' is treated as a double data type anyway.

Save, compile, and execute the program.

Result:

19.95
19.95

float data type vs double data type

The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float data type is about six or seven decimal digits, while double data type has a precision of about 15 digits.

Therefore it is safer to use double for most calculations.

Note: A floating point number can also be a scientific number with an "e" to indicate the power of 10. e.g float num = 21e7f;

Characters in Java Primitive Data Type

The char data type is a single 16-bit Unicode character used to specify any single character. It ranges from '\u0000' as the minimun value to '\uffff' as the maximum value.

A char value must be surrounded by single quotes, like 'A' or 'f':

Save, compile, and execute the program.

Result:

A
D

Booleans in Java Primitive Data Type

A boolean data type is specified with the boolean keyword and can only take the values true or false.

They are mostly used for conditional testing, which you will learn more about in upcoming tutorials.

Save, compile, and execute the program.

Result:

true
false

Non-Primitive Data Types

Non-primitive data types are called reference types because they refer to objects. Apart from the String data type, which is predefined in Java, non-primitive data types are created using constructors of the classes and used to access objects. When used on identifiers like variables, they cannot be changed because they are declared to be of a specific type.

Examples of non-primitive types are Strings, Arrays, Classes, Interfaces, etc. You will learn more about these in upcoming tutorials.

Strings in Java Non-Primitive Data type

The String data type is used to specify a sequence of characters that may appear as text. String values must be surrounded by double quotes. The String data type is so much used in Java, that some people mistake it for a primitive data type.

However, it is actually a non-primitive data type, because it refers to an object. The String object has methods that is used to perform certain operations on its values.

Save, compile, and execute the program.

Result:

Hello World

Cheers! From Sikademy.

You will learn more about each of these Data Types in a later tutorial.

Tutorial Exercise

1. Assign data types to the following identifier and print the result. Remember to add semi-colon at the end of each.

name = "Archangel Macsika"

age = "105"

secondAge = 180

amountInAccount = 0.55

needsHowMuchMoney = 9923000000000000

isAHuman = "Kinda"

wakesAt = '5'

likesFood = True

goesToTheMarket = False


Enjoy this Tutorial? Please don't forget to share.