Core Java Programming: From Beginner to Project Development

Lesson 2.2: Primitive Data Types in Java

Introduction

In the previous lesson, you learned how to create variables. However, before storing any value in a variable, Java needs to know what type of data will be stored.

For example:

  • A student’s age is a whole number.

  • A student’s percentage may contain decimal values.

  • A person’s name is text.

  • A true or false value represents a condition.

To handle different kinds of information, Java provides Data Types.

A data type tells the compiler:

  • What kind of value a variable can store.

  • How much memory should be allocated.

  • What operations can be performed on that value.

In this lesson, you will learn about Java’s Primitive Data Types, which are the basic building blocks of Java programming.


Learning Objectives

After completing this lesson, you will be able to:

  • Understand what a data type is.

  • Explain primitive data types.

  • Declare variables using different primitive data types.

  • Choose the correct data type for different situations.

  • Write programs using primitive data types.


What is a Data Type?

A Data Type defines the type of value that a variable can store.

For example:

int age = 20;

Here,

  • int is the data type.

  • age is the variable.

  • 20 is the value.

Since age stores a whole number, the int data type is used.


Categories of Data Types

Java data types are divided into two categories:

  1. Primitive Data Types

  2. Non-Primitive (Reference) Data Types

In this lesson, we will study Primitive Data Types.


What are Primitive Data Types?

Primitive data types are the built-in data types provided by Java.

They store simple values such as numbers, characters, and boolean values.

Java has 8 Primitive Data Types.

Data Type Size Default Value Example
byte 1 Byte 0 100
short 2 Bytes 0 25000
int 4 Bytes 0 150
long 8 Bytes 0L 500000L
float 4 Bytes 0.0f 45.5f
double 8 Bytes 0.0 45.5678
char 2 Bytes ‘\u0000’ ‘A’
boolean 1 Bit (logical) false true

Integer Data Types

These data types store whole numbers.

1. byte

Range:

-128 to 127

Example:

byte age = 25; System.out.println(age);

Use byte when memory usage is important.


2. short

Range:

-32,768 to 32,767

Example:

short salary = 25000; System.out.println(salary);

3. int

The most commonly used integer type.

Example:

int marks = 95; System.out.println(marks);

4. long

Used for storing very large integers.

Example:

long population = 1400000000L; System.out.println(population);

Notice the L at the end of the value.


Floating-Point Data Types

Used to store decimal numbers.


5. float

Example:

float percentage = 89.5f; System.out.println(percentage);

Notice the f at the end.


6. double

More accurate than float.

Example:

double pi = 3.1415926535; System.out.println(pi);

double is the default choice for decimal values.


Character Data Type

7. char

Stores a single character.

Example:

char grade = 'A'; System.out.println(grade);

Characters must be enclosed in single quotes (‘ ‘).


Boolean Data Type

8. boolean

Stores only two values:

  • true

  • false

Example:

boolean passed = true; System.out.println(passed);

Boolean values are commonly used in decision-making statements.


Example Program

public class DataTypesExample { public static void main(String[] args) { byte age = 20; short year = 2025; int marks = 95; long population = 1400000000L; float percentage = 89.5f; double pi = 3.1415926535; char grade = 'A'; boolean passed = true; System.out.println("Age: " + age); System.out.println("Year: " + year); System.out.println("Marks: " + marks); System.out.println("Population: " + population); System.out.println("Percentage: " + percentage); System.out.println("PI: " + pi); System.out.println("Grade: " + grade); System.out.println("Passed: " + passed); } }

Choosing the Correct Data Type

Situation Recommended Data Type
Age int
Marks int
Percentage float or double
Price double
Grade char
True/False Condition boolean
Population long

Common Mistakes

Forgetting f with float

Incorrect:

float price = 99.5;

Correct:

float price = 99.5f;

Forgetting L with long

Incorrect:

long number = 9876543210;

Correct:

long number = 9876543210L;

Using Double Quotes for Characters

Incorrect:

char grade = "A";

Correct:

char grade = 'A';

Best Practices

  • Use int for most whole numbers.

  • Use double for decimal values unless memory is limited.

  • Use meaningful variable names.

  • Choose the smallest suitable data type for better memory efficiency.


Key Points to Remember

  • Java has 8 primitive data types.

  • Primitive data types store simple values.

  • int is the most commonly used integer type.

  • double is preferred for decimal numbers.

  • char stores a single character.

  • boolean stores either true or false.


Lesson Summary

In this lesson, you learned:

  • What a data type is.

  • The eight primitive data types in Java.

  • The purpose of each primitive data type.

  • How to declare variables using primitive data types.

  • How to select the appropriate data type based on the type of data.

Primitive data types form the foundation of Java programming and are used in almost every Java application.


Practice Questions

  1. What is a data type?

  2. How many primitive data types are available in Java?

  3. Which data type is used for decimal values?

  4. Which data type stores a single character?

  5. What is the difference between float and double?

  6. Why do we use the boolean data type?


Programming Exercise

Write separate Java programs to:

  1. Store your age using int.

  2. Store your percentage using float.

  3. Store your grade using char.

  4. Store your pass/fail status using boolean.

  5. Display all values using System.out.println().


Assignment

Create a program named StudentDetails.java that stores and displays the following information using appropriate primitive data types:

  • Student Name (use String for now)

  • Age

  • Roll Number

  • Percentage

  • Grade

  • Fees

  • Pass Status

Print the output in a neatly formatted manner.

Next Lesson: Lesson 2.3 – Non-Primitive Data Types

Scroll to Top