📘LESSON 3
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define primitive data types in Java.
- Identify the different primitive data types.
- Declare and initialize primitive variables.
- Differentiate between primitive and reference data types.
- Use primitive data types in Java applications.
3.2 Overview
Data types are used in Java to define the kind of data stored in variables. Primitive data types are the most basic data types in Java and are used to store simple values such as numbers, characters, and logical values.
This lesson introduces learners to Java primitive data types and explains how primitive values are stored and processed during program execution. Learners will also explore how primitive data types are used in calculations, comparisons, conditions, and application logic.
Understanding primitive data types is essential because they are used throughout all Java applications and form the foundation of data processing in Java programming.
KT0301 — Introduction to Primitive Data Types
Primitive data types are predefined data types built into the Java language.
Primitive data types:
- store simple values directly in memory,
- are faster than reference types,
- and are commonly used for calculations and logical operations.
Java contains eight primitive data types.
The Eight Primitive Data Types
| Data Type | Description | Example |
|---|---|---|
| byte | Small integer | 10 |
| short | Short integer | 200 |
| int | Integer number | 5000 |
| long | Large integer | 100000L |
| float | Decimal number | 5.5f |
| double | Large decimal number | 19.99 |
| char | Single character | ‘A’ |
| boolean | True or false value | true |
KT0302 — Integer Data Types
Integer data types are used to store whole numbers.
Java provides four integer types:
- byte,
- short,
- int,
- and long.
Example
public class Main {
public static void main(String[] args) {
byte age = 20;
short marks = 300;
int salary = 5000;
long population = 1000000L;
System.out.println(age);
System.out.println(marks);
System.out.println(salary);
System.out.println(population);
}
}
Output
20
300
5000
1000000
Importance of Integer Types
Integer data types are commonly used for:
- counting,
- calculations,
- IDs,
- quantities,
- and application statistics.
KT0303 — Decimal Data Types
Decimal data types are used to store numbers with decimal values.
Java provides:
- float,
- and double.
The double type is more precise than float.
Example
public class Main {
public static void main(String[] args) {
float temperature = 25.5f;
double price = 199.99;
System.out.println(temperature);
System.out.println(price);
}
}
Output
25.5
199.99
Importance of Decimal Types
Decimal types are used in:
- banking systems,
- scientific calculations,
- inventory systems,
- and financial applications.
KT0304 — Character Data Type
The char data type stores a single character.
Characters are enclosed using single quotation marks.
Example
public class Main {
public static void main(String[] args) {
char grade = 'A';
System.out.println(grade);
}
}
Output
A
Uses of Characters
Characters are commonly used for:
- grades,
- symbols,
- initials,
- and menu selections.
KT0305 — Boolean Data Type
The boolean data type stores logical values.
A boolean variable can only contain:
- true,
- or false.
Example
public class Main {
public static void main(String[] args) {
boolean isLoggedIn = true;
System.out.println(isLoggedIn);
}
}
Output
true
Importance of Boolean Values
Boolean values are used in:
- conditions,
- decision making,
- authentication systems,
- and program control logic.
KT0306 — Primitive vs Reference Data Types
Primitive data types:
- store simple values directly,
- and use less memory.
Reference data types:
- store references to objects,
- and are used for complex data structures.
Primitive Example
int number = 10;
Reference Example
String name = "Java";
Differences Between Primitive and Reference Types
| Primitive Types | Reference Types |
|---|---|
| Store actual values | Store object references |
| Faster processing | More flexible |
| Fixed size | Dynamic structures |
| Built into Java | Created using classes |
3.5 Key Notes / Summary
- Primitive data types are built into Java.
- Java contains eight primitive data types.
- Integer types store whole numbers.
- float and double store decimal values.
- char stores single characters.
- boolean stores true or false values.
- Primitive types differ from reference types.