Programming and Java Fundamentals

Primitive Data Types

ReadingPreview

আপনি একটি free preview lesson দেখছেন।

Lesson Overview

In Java, every variable stores a specific type of value.

For example:

  • Whole numbers
  • Decimal numbers
  • A character
  • true or false

To represent these basic values, Java provides eight primitive data types:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

In this lesson, we will understand each primitive type, its range, literal syntax, and practical use.

Learning Objectives

After completing this lesson, you will be able to:

  • Explain what a primitive data type is
  • Identify Java's eight primitive types
  • Select an appropriate integer type for whole numbers
  • Understand the difference between float and double
  • Use char and boolean
  • Understand why L and F suffixes are needed
  • Use underscores in numeric literals
  • Understand the basic concept of integer overflow
  • Explain floating-point precision limitations
  • Select the appropriate primitive type based on the requirement

What Is a Data Type?

A data type determines what kind of value a variable can store.

Example:

int age = 30;

Here, int indicates that age will store an integer value.

double averageMark = 85.5;

Here, double stores a decimal value.

boolean isAvailable = true;

Here, boolean stores a logical state.

What Is a Primitive Data Type?

A primitive data type is a built-in fundamental type in the Java language.

Primitive types are not objects; they represent basic values.

int age = 30;
char grade = 'A';
boolean isPassed = true;

Java's eight primitive types can be divided into four categories:

CategoryTypes
Integerbyte, short, int, long
Floating-pointfloat, double
Characterchar
Booleanboolean

Primitive Types at a Glance

TypeSizeExampleCommon Use
byte8 bits100Very small integers or binary data
short16 bits1200Small integers
int32 bits5000General whole numbers
long64 bits8_000_000_000LLarge whole numbers
float32 bits10.5FLower-precision decimals
double64 bits10.5General decimal calculations
char16 bits'A'A single UTF-16 code unit
boolean—trueTrue/false state

The Java language does not define a fixed storage size for boolean, so there is no need to memorize a specific bit size for it.

Integer Types

Java's integer primitive types are:

byte
short
int
long

These types store whole numbers without a decimal part.

Examples:

-20
0
25
1000
5000000

byte

byte is an 8-bit signed integer.

Range:

-128 to 127

Example:

byte percentage = 100;
byte temperature = -10;

A value outside this range cannot be stored directly:

byte value = 128;

This will not compile.

When Should You Use byte?

byte is not commonly needed for general application data.

It can be useful for:

  • Raw binary data
  • File or network bytes
  • Memory-sensitive large arrays
  • External formats that require 8-bit values

short

short is a 16-bit signed integer.

Range:

-32,768 to 32,767

Example:

short roomNumber = 1200;
short maximumScore = 30000;

short is also not commonly used in general Java applications.

int

int is a 32-bit signed integer.

Range:

-2,147,483,648
to
2,147,483,647

For general whole numbers, int is the most common choice in Java.

int age = 30;
int studentCount = 500;
int lessonCount = 120;
int quantity = 5;

When Should You Use int?

In general, int can be used for values such as:

  • Age
  • Quantity
  • Marks
  • Retry count
  • Page number
  • Lesson count
  • Inventory count
  • General counters

long

long is a 64-bit signed integer.

Range:

-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807

Use long for whole numbers that are larger than the int range.

long totalUsers = 5_000_000_000L;
long fileSizeInBytes = 8_000_000_000L;

L Suffix

Use an uppercase L at the end of large long literals:

long population = 8_000_000_000L;

L tells the compiler that the literal is a long.

Although lowercase l is technically allowed, avoid it:

long value = 100l;

It can look similar to the digit 1.

Preferred:

long value = 100L;

Choosing an Integer Type

Practical rule:

  • General whole number → int
  • If the int range is not enough → long
  • byte or short → only when there is a specific reason

Example:

int studentAge = 20;
int lessonCount = 120;
long totalPlatformUsers = 5_000_000_000L;

Underscores in Numeric Literals

Underscores can be used between digits to make large numbers easier to read.

int oneMillion = 1_000_000;
long population = 8_000_000_000L;

The compiler ignores these underscores.

int value = 1_000;

and:

int value = 1000;

represent the same value.

Integer Division

When two integers are divided, the result follows integer arithmetic.

int result = 5 / 2;

System.out.println(result);

Output:

2

Not 2.5.

To get a decimal result, at least one operand must be floating-point:

double result = 5.0 / 2;

System.out.println(result);

Output:

2.5

Integer Overflow

If the result exceeds the maximum range of an integer type, overflow can occur.

int value = 2_147_483_647;

value = value + 1;

System.out.println(value);

Output:

-2147483648

The value wraps after reaching the maximum.

This is important because Java integer overflow does not always produce an exception.

When working with large values, you should understand the range of the selected type.

Literal Type Also Matters

Consider this calculation:

long total = 2_000_000_000 + 1_000_000_000;

Even though the result variable is long, the addition can first be performed using int arithmetic.

Safer:

long total = 2_000_000_000L + 1_000_000_000L;

Now the calculation uses the long range.

Floating-Point Types

Java provides two primitive types for decimal or fractional values:

  • float
  • double

float

float is a 32-bit floating-point type.

Its approximate precision is generally 6–7 significant decimal digits.

Example:

float temperature = 36.5F;

Decimal literals are double by default, so a float literal needs an F suffix.

Correct:

float temperature = 36.5F;

Invalid:

float temperature = 36.5;

double

double is a 64-bit floating-point type.

It provides more precision than float and is more commonly used for general decimal calculations in Java.

double averageMark = 85.75;
double temperature = 36.5;
double distance = 12.75;

Decimal literals are double by default, so no suffix is required.

float or double?

TypeSizeApproximate Precision
float32 bits6–7 significant digits
double64 bits15–16 significant digits

General rule:

Use double for general decimal calculations.

float is useful when 32-bit floating-point is specifically required or memory usage is important.

Floating-Point Precision

float and double represent decimal values using binary floating-point format.

Not every decimal number can be represented exactly in binary.

Example:

double result = 0.1 + 0.2;

System.out.println(result);

Output may be:

0.30000000000000004

This is not a Java bug.

It is a limitation of floating-point representation.

Why Should We Avoid double for Money?

For exact values such as money, floating-point approximation can create problems.

Risky:

double amount = 0.1 + 0.2;

In this course, we will represent monetary values exactly using integer minor units.

Example:

long priceInPaisa = 499_050L;

If:

100 paisa = 1 BDT

then:

499,050 paisa = BDT 4,990.50

Similarly, for a cent-based currency:

long priceCents = 4_990L;

It is important to make the monetary unit clear in the variable name.

char

char represents a single UTF-16 code unit.

The literal is written inside single quotes:

char grade = 'A';
char symbol = '#';
char banglaLetter = 'ক';

Single character:

'A'

String:

"Java"

These are not the same.

char and String

char firstLetter = 'J';
String language = "Java";

A char stores one code unit.

A String represents a sequence of text and is not a primitive type.

This distinction will become clearer in the later String lesson.

Invalid char

Wrong:

char language = 'Java';

Correct:

char firstLetter = 'J';

or:

String language = "Java";

boolean

boolean represents a logical state.

Its possible values are:

true
false

Example:

boolean isAvailable = true;
boolean paymentCompleted = false;
boolean hasPermission = true;

In Java, a number cannot be used as a boolean.

Invalid:

boolean isActive = 1;

Correct:

boolean isActive = true;

Similarly:

boolean isActive = "true";

is invalid because "true" is a String.

Boolean Variable Naming

Boolean variable names should make the condition clear.

Good:

boolean isActive;
boolean isAvailable;
boolean hasPermission;
boolean canEnroll;
boolean paymentCompleted;

Less clear:

boolean flag;
boolean value;
boolean status;

Primitive and Reference Types

A primitive type represents a basic value:

int age = 30;
double average = 85.5;
char grade = 'A';
boolean passed = true;

String is not a primitive type.

String studentName = "Sakib";

String is a reference type.

For now, remember this distinction:

Primitive → basic value
Reference type → object/reference-based value

We will understand reference types in more detail when we learn object-oriented programming.

Choosing the Correct Data Type

When choosing a data type, consider the nature of the value and its possible range.

Student Age

int age = 20;

Even though the value would fit inside a byte, int is generally more practical for normal application code.

Lesson Count

int lessonCount = 50;

Large User Count

long userCount = 5_000_000_000L;

Average Mark

double averageMark = 85.67;

Grade

char grade = 'A';

Availability

boolean isAvailable = true;

Exact Money

long priceCents = 499_000L;

or another minor unit depending on the currency:

long priceInPaisa = 499_000L;

Primitive Type Selection Guide

RequirementSuggested Type
General whole numberint
Very large whole numberlong
General decimal calculationdouble
32-bit approximate decimal requiredfloat
Single UTF-16 code unitchar
True/false stateboolean
Exact monetary valuelong minor units
Raw 8-bit binary valuebyte

Common Errors

Value Outside the byte Range

Wrong:

byte value = 200;

Better:

int value = 200;

Missing L in a long Literal

Wrong:

long population = 8_000_000_000;

Correct:

long population = 8_000_000_000L;

Missing F in a float Literal

Wrong:

float temperature = 36.5;

Correct:

float temperature = 36.5F;

Integer Division

double result = 5 / 2;

Result:

2.0

because 5 / 2 performs integer division first.

Correct:

double result = 5.0 / 2;

Result:

2.5

Wrong Quotes for char

Wrong:

char grade = "A";

Correct:

char grade = 'A';

Wrong Boolean Value

Wrong:

boolean active = 1;

Correct:

boolean active = true;

Integer Overflow

Risky:

int total = 2_000_000_000 + 1_000_000_000;

Use a wider type:

long total = 2_000_000_000L + 1_000_000_000L;

Complete Example

public class Main {

    public static void main(String[] args) {
        String studentName = "Sakib";

        int mathematicsMark = 80;
        int englishMark = 90;
        int scienceMark = 85;

        int totalMarks =
                mathematicsMark
                + englishMark
                + scienceMark;

        double averageMark = totalMarks / 3.0;
        char grade = 'A';
        boolean isPassed = true;

        System.out.println("Student: " + studentName);
        System.out.println("Total: " + totalMarks);
        System.out.println("Average: " + averageMark);
        System.out.println("Grade: " + grade);
        System.out.println("Passed: " + isPassed);
    }
}

Output:

Student: Sakib
Total: 255
Average: 85.0
Grade: A
Passed: true

In this program:

  • Marks → int
  • Average → double
  • Grade → char
  • Passed status → boolean
  • Student name → String, which is not primitive

Important Terms

Primitive Data Type

A built-in fundamental Java type that represents a basic value.

Integer

A whole number without a decimal part.

Floating-Point

An approximate numeric type used to represent decimal or fractional values.

Range

The minimum and maximum values a type can store.

Precision

The number of significant digits a floating-point type can represent.

Literal

A value written directly in source code.

100
10.5
'A'
true

Overflow

When a numeric result exceeds the maximum range of its type and wraps around.

Integer Division

Division using integer operands where the fractional part is discarded.

Floating-Point Precision

The approximation of some decimal values because of binary representation.

Practice Exercise 1: Select the Type

Choose a suitable type for each value:

  1. Student age
  2. Course lesson count
  3. 5 billion users
  4. Average mark
  5. Grade A
  6. Whether a user is active
  7. File size in bytes
  8. Temperature
  9. Maximum login attempts
  10. A Bangla letter

Practice Exercise 2: Literal Type

Write the usual type of each literal:

100
100L
10.5
10.5F
'A'
true

Practice Exercise 3: Find the Invalid Declarations

byte age = 25;
byte value = 200;
int lessonCount = 120;
long population = 8_000_000_000L;
float temperature = 36.5F;
double average = 81.67;
char grade = "A";
boolean available = true;
boolean active = 1;

Correct the invalid declarations.

Practice Exercise 4: Integer Division

Predict the output:

int firstResult = 5 / 2;
double secondResult = 5 / 2;
double thirdResult = 5.0 / 2;

System.out.println(firstResult);
System.out.println(secondResult);
System.out.println(thirdResult);

Practice Exercise 5: Prevent Overflow

Correct the following code:

long total = 2_000_000_000 + 2_000_000_000;

Expected result:

4000000000

Practice Exercise 6: Represent Money

Course price:

BDT 4,990.50

Assume:

1 BDT = 100 paisa

Answer:

  1. What is the value in paisa?
  2. Which primitive type would you use?
  3. What would be a meaningful variable name?

Practice Exercise 7: Student Result

Write a program containing:

  • Student name → String
  • Mathematics mark → int
  • English mark → int
  • Science mark → int
  • Total → int
  • Average → double
  • Grade → char
  • Passed → boolean

You may use your own values.

Knowledge Check

Question 1

How many primitive data types are there in Java?

Question 2

What are the eight primitive types?

Question 3

What are the integer primitive types?

Question 4

Which type is generally used for ordinary whole numbers?

Question 5

Which type is used for whole numbers larger than the int range?

Question 6

What suffix is used for a long literal?

Question 7

What type is a decimal literal by default?

Question 8

What suffix is used for a float literal?

Question 9

Which is more common for general decimal calculations: float or double?

Question 10

What is the result of 5 / 2?

Question 11

What is integer overflow?

Question 12

Why might 0.1 + 0.2 not be exactly 0.3?

Question 13

What will we use in this course to represent exact monetary values?

Question 14

Which quotation marks does char use?

Question 15

What are the possible values of boolean?

Question 16

Is String a primitive type?

Knowledge Check Answers

Answer 1

Java has eight primitive data types.

Answer 2

byte
short
int
long
float
double
char
boolean

Answer 3

byte
short
int
long

Answer 4

int is generally used for ordinary whole numbers.

Answer 5

long is used.

Answer 6

L

Answer 7

A decimal literal is double by default.

Answer 8

F

Answer 9

double is more common for general decimal calculations.

Answer 10

2

because this is integer division.

Answer 11

Integer overflow occurs when a numeric result exceeds the maximum range of its integer type and wraps around.

Answer 12

Because double uses binary floating-point representation, and not every decimal value can be represented exactly.

Answer 13

We will use integer minor units, generally stored in a long, for exact monetary values.

Answer 14

Single quotes:

'A'

Answer 15

true
false

Answer 16

No. String is a reference type.

Lesson Summary

In this lesson, we learned:

  • Java has eight primitive data types
  • byte, short, int, and long store whole numbers
  • int is the most common choice for general integers
  • long is used for larger integers
  • L is used at the end of long literals
  • float and double represent decimal values
  • Decimal literals are double by default
  • float literals require an F suffix
  • double is preferred for general decimal calculations
  • Integer division discards the fractional part
  • Integer overflow can produce incorrect values
  • float and double represent approximate floating-point values
  • Integer minor units should be used for exact monetary values
  • char represents a single UTF-16 code unit
  • boolean stores only true or false
  • String is not a primitive type
  • Choosing the correct data type is important for correctness and readability