Ultimate Tutorial on Data types in Java Programming

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 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 include 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 type | Non-primitive Data type |
---|---|
Primitive types are predefined (already defined) in Java. | Non-primitive types are created by the programmer and are 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 has a value, and cannot be null. | Non-primitive types can be null. |
A primitive type starts with a lowercase letter. | Non-primitive types start 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 Type | Size | Description |
---|---|---|
byte | 1 byte | Stores whole numbers from -128 to 127 |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775.808 to 9,223.372,036,854,775,808 |
float | 4 bytes | Stores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers from 1.7e−308 to 1.7e+038. Sufficient for storing 15 decimal digits. |
boolean | 1 byte | Stores true or false values |
char | 2 bytes | Stores 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
The 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.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
byte age = 80;
System.out.println(age);
}
}
Save, compile, and execute the program.
Result:
- Output
Short written as short
The 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.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
short numberOfBooksInLibrary = 8000;
System.out.println(numberOfBooksInLibrary);
}
}
Save, compile, and execute the program.
Result:
- Output
Int written as int
The 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.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
int populationOfUsa = 328087772; //est. as of January 2019
System.out.println(populationOfUsa);
}
}
Save, compile, and execute the program.
Result:
- Output
Long written as long
The 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
- Java Code
public class TutorialClass {
public static void main(String[] args) {
long gdpOfUsa = 19390000000000L; //est. as of January 2019
System.out.println(gdpOfUsa);
}
}
Save, compile, and execute the program.
Result:
- Output
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
The 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 is 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.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
float cost = 204.53f;
System.out.println(cost);
}
}
Save, compile, and execute the program.
Result:
- Output
Double written as double
The 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 optional because a decimal without an 'f' or a 'd' is treated as a double
data type anyway.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
double allowanceOne = 120.95d;
double allowanceTwo = 120.95;
System.out.println(allowanceOne + "\n\n" + allowanceTwo);
}
}
Save, compile, and execute the program.
Result:
- Output
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 the float
data type is about six or seven decimal digits, while the 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 minimum value to '\uffff' as the maximum value.
A char
value must be surrounded by single quotes, like 'A' or 'f':
- Java Code
public class TutorialClass {
public static void main(String[] args) {
char studentOneGrade = 'A';
char studentTwoGrade = 'D';
System.out.println(studentOneGrade);
System.out.println(studentTwoGrade);
}
}
Save, compile, and execute the program.
Result:
- Output
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.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
boolean isProgrammingFun = true;
boolean isFightingFun = false;
System.out.println(isProgrammingFun);
System.out.println(isFightingFun);
}
}
Save, compile, and execute the program.
Result:
- Output
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 are used to perform certain operations on its values.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
String welcomeMessage = "Hello World\n\nCheers! From Sikademy.";
System.out.println(welcomeMessage);
}
}
Save, compile, and execute the program.
Result:
- Output
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 a 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