OOP with Java – Detailed Lecture
Topic: Variables → Data Types (In Full Depth)
Explore the fundamental concepts of variables and data types in Java, essential for all aspiring programmers.
Approach: Concept → Real Life → Memory → Code
1 2 3
Understand Variables Declare and Use Variables Data Types with Memory &
Grasp what variables are and their Learn the correct syntax and best
Range
crucial role in programming. practices for declaring and utilizing Dive deep into data types,
variables. understanding their memory
allocation and value ranges.
4 5
Choose Correct Data Types Primitive vs. Non-Primitive
Develop the ability to select appropriate data types for Distinguish between primitive and non-primitive data types
various real-world scenarios. and their implications.
CHAPTER 1
1. VARIABLES (FOUNDATION FIRST)
1.1 What is a Variable?
Definition
A variable is a named memory location that stores data whose value can change during program execution.
Real-World Analogy: Labeled Container
Imagine a cupboard with drawers, each labeled to hold specific items:
Drawer Label Stored Value Java Equivalent
name "Ali" String name = "Ali";
age 20 int age = 20;
cgpa 3.29 double cgpa = 3.29;
Label→ variable name (e.g., name)
Item → value (e.g., "Ali")
Drawer size → data type (e.g., String)
1.2 Why Variables Are Needed
Variables are essential for creating dynamic and interactive programs. They allow applications to store and manipulate information,
making them useful for a wide range of real-world applications:
Student record systems (storing names, grades)
Bank accounts (tracking balances, transactions)
Shopping carts (managing items, quantities)
Attendance systems (recording presence/absence)
Without variables, programs would be static, performing the
same actions every time. With variables, programs become
dynamic, adapting to user input and changing conditions.
1.3 Structure of a Variable
dataType variableName = value;
Example
int marks = 85;
Part Meaning
int Data type (specifies memory allocation and type of data)
marks Variable name (identifier for the memory location)
85 Value (the data stored in the memory location)
1.4 Variable Value Can Change
The term "variable" itself highlights its fundamental characteristic: its value can be modified during program execution.
int score = 50;
score = 75;
Here, the score variable initially holds the value 50, which is then updated to 75. This ability to change values is what makes variables
indispensable for dynamic programming.
1.5 First Simple Program Using Variables
Let's see a basic Java program that declares and prints a variable:
class VariableIntro {
public static void main(String[] args) {
int age = 21; // Declares an integer variable 'age' and assigns it the value 21
[Link](age); // Prints the current value of 'age' to the console
}
}
CHAPTER 2
2. DATA TYPES (CORE CONCEPT)
2.1 What is a Data Type?
A data type is a classification that tells the compiler or interpreter how the programmer intends to use the data. In Java, a data type
defines:
Kind of Data: What type of values can be stored (e.g., numbers, text, true/false).
Memory Allocation: How much memory space to reserve for the variable.
Allowed Operations: Which operations can be performed on the data.
2.2 Categories of Data Types
Java organizes data types into two main categories, each serving distinct purposes in programming:
Primitive Data Types Non-Primitive Data Types
These types store the actual values directly in the memory Also known as Reference Types, these store memory
location allocated for the variable. They are fundamental addresses (references) to where the actual data is stored in
building blocks. the heap. They refer to objects.
Examples: int, char, boolean Examples: String, Arrays, Classes
CHAPTER 3
3. PRIMITIVE DATA TYPES (WITH FULL MEMORY
DETAILS)
Primitive data types are the most basic data types available in Java. They are predefined and have a reserved keyword.
3.1 Integer Data Types (Whole Numbers)
These types are used to store whole numbers without any decimal points.
1 2
byte short
Memory: 1 byte (8 bits) Memory: 2 bytes (16 bits)
Range: –128 to 127 Range: –32,768 to 32,767
byte temperature = 25; short year = 2025;
Used rarely, mainly for memory optimization in large data Suitable for small numerical values where int would be
structures like arrays. overkill.
3 4
int (MOST USED) long
Memory: 4 bytes (32 bits) Memory: 8 bytes (64 bits)
Range: –2,147,483,648 to 2,147,483,647 Used for very large whole numbers.
int age = 20; long population = 241000000L;
int students = 45;
The suffix L (or l) is mandatory to denote a long literal.
This is the default integer data type in Java, offering a good
balance of range and memory.
3.2 Decimal (Floating-Point) Data Types
Used for numbers with fractional parts, offering precision for calculations.
float double (MOST USED FOR DECIMALS)
Memory: 4 bytes Memory: 8 bytes
Precision: ~6–7 decimal digits Precision: ~15 decimal digits
float temperature = 36.6f; double cgpa = 3.29;
double salary = 85000.75;
The suffix f (or F) is mandatory for float literals. This is generally
used when memory is a critical concern, or for simpler scientific This is the default decimal data type in Java, providing high
calculations. precision for most financial and scientific computations.
char 3.4 Boolean Data Type
Memory: 2 bytes (16 bits) boolean
Reason: Unicode support
Memory: JVM dependent (typically 1 byte)
Values: true or false
char grade = 'A';
char gender = 'M';
boolean isPassed = true;
boolean isFeePaid = false;
char uses 2 bytes to support a wide range of characters from
various languages (English, Urdu, Arabic, Chinese, etc.), making
Java globally compatible. Booleans are fundamental for decision-making logic and
controlling program flow.
Example: Primitive Data Types Program
Here's a small program demonstrating the declaration and usage of various primitive data types:
class PrimitiveExample {
public static void main(String[] args) {
int age = 21; // Integer for whole number
double cgpa = 3.45; // Double for decimal number
char grade = 'A'; // Character for single letter
boolean pass = true; // Boolean for true/false
[Link]("Age: " + age);
[Link]("CGPA: " + cgpa);
[Link]("Grade: " + grade);
[Link]("Passed: " + pass);
}
}
CHAPTER 4
4. NON-PRIMITIVE DATA TYPES (REFERENCE TYPES)
4.1 Key Difference
Primitive Types Non-Primitive Types
Directly store the actual value in the memory location Store the memory address (a reference) to where the
allocated for the variable. actual object data resides in the heap memory.
4.2 String Data Type (MOST IMPORTANT)
The String class is one of the most commonly used non-primitive data types in Java, representing sequences of characters.
String name = "Aqib Ali";
Memory Explanation
The variable name stores a reference (memory address).
The actual string value "Aqib Ali" is stored in the heap memory.
Real-World Usage
Strings are fundamental for handling textual information:
Names (e.g., "John Doe")
Emails (e.g., "[Link]@[Link]")
Addresses (e.g., "123 Main St")
Passwords (e.g., "securePass123")
4.3 char vs String
char String
Single character Multiple characters
'A' "Ali"
Primitive Non-primitive (Reference type)
CHAPTER 5
5. WHY CORRECT DATA TYPE MATTERS
Choosing the correct data type is critical for efficient and error-free programming.
Wrong Choice Example
int price = 99.99; //
5.1 Type Casting (Memory Conversion)
Type casting allows you to convert a value from one data type to another.
Implicit Casting (Widening Conversion) Explicit Casting (Narrowing Conversion)
Happens automatically when converting from a smaller to a Requires manual intervention using parentheses, as data loss
larger data type, as no data is lost. might occur when converting from a larger to a smaller data
type.
int a = 10;
double b = a; // 'a' (int) is implicitly converted to double double x = 9.8;
int y = (int) x; // 'x' (double) is explicitly cast to int, resulting in
y=9
CHAPTER 6
6. REAL-WORLD PROGRAM (STUDENT SYSTEM)
This program demonstrates how different data types are used together to represent student information:
class StudentSystem {
public static void main(String[] args) {
String name = "Sara"; // String for student's name
int rollNo = 101; // int for roll number
double cgpa = 3.75; // double for GPA
char grade = 'A'; // char for letter grade
boolean promoted = true; // boolean for promotion status
[Link]("Student Name: " + name);
[Link]("Roll Number: " + rollNo);
[Link]("CGPA: " + cgpa);
[Link]("Grade: " + grade);
[Link]("Promoted: " + promoted);
}
}