1. What is a Variable
1.1 Definition
A variable is a container used to store data that can be used and modified during program execution.
1.2 Purpose of Variables
- Store values (numbers, text, etc.)
- Reuse data in programs
- Make programs dynamic and flexible
1.3 Memory Representation
When a variable is created, memory is allocated in the system to store its value.
1.4 Naming Rules
- Must start with a letter,
_, or$ - Cannot start with a number
- Cannot use Java keywords
- Case-sensitive
1.5 Naming Conventions
- Use meaningful names
- Follow camelCase (e.g.,
studentName,totalMarks)
2. Primitive Data Types
2.1 Overview
Primitive data types are basic data types built into Java. They store simple values directly.
2.2 int
Used to store whole numbers.
int age = 25;
- Size: 4 bytes
- Range: -2,147,483,648 to 2,147,483,647
2.3 float
Used to store decimal numbers (single precision).
float price = 99.99f;
- Size: 4 bytes
- Requires
fsuffix
2.4 double
Used for decimal numbers with higher precision.
double pi = 3.14159;
- Size: 8 bytes
- More precise than float
2.5 char
Used to store a single character.
char grade = 'A';
- Size: 2 bytes
- Uses Unicode
2.6 boolean
Used to store true or false values.
boolean isActive = true;
2.7 byte
Used to store small integer values.
byte level = 10;
- Size: 1 byte
- Range: -128 to 127
2.8 short
Used for small integer values.
short year = 2025;
- Size: 2 bytes
2.9 long
Used for large integer values.
long population = 9000000000L;
- Size: 8 bytes
- Requires
Lsuffix
3. Non-Primitive Data Types
3.1 Definition
Non-primitive data types store references to objects rather than actual values.
3.2 Differences
| Primitive | Non-Primitive |
|---|---|
| Stores value | Stores reference |
| Fixed size | Variable size |
| No methods | Has methods |
3.3 Common Types
- String
- Arrays
- Classes
- Objects
3.4 Memory Storage
- Primitive → Stack memory
- Non-Primitive → Heap memory
3.5 Example
String name = "Pooja";
4. Declaring and Initializing Variables
4.1 Declaration
Declaring a variable means specifying its type and name.
int number;
4.2 Initialization
Assigning a value to a variable.
number = 10;
4.3 Declaration + Initialization
int number = 10;
4.4 Multiple Variables
int a = 1, b = 2, c = 3;
4.5 Constants
Use final keyword.
final double PI = 3.14;
5. Type Casting
5.1 Introduction
Type casting is the process of converting one data type into another.
5.2 Implicit Casting (Widening)
Definition
Automatic conversion from smaller type to larger type.
Example
int num = 10;
double value = num;
- No data loss
- Done automatically
5.3 Explicit Casting (Narrowing)
Definition
Manual conversion from larger type to smaller type.
Example
double num = 10.5;
int value = (int) num;
- May cause data loss
- Requires casting operator
Common Type Conversion Order
byte → short → int → long → float → double
Conclusion
Variables and data types are the building blocks of Java programming. Once you understand how to store data, choose the right type, and convert between types, you can start building meaningful programs. These concepts will be used in almost every Java application you create.