1. Variables áááŻáᏠááŹáá˛?
A variable is a container that stores data values during program execution.
int age = 25;
String name = "John";
double salary = 5000.50;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
Variable áááŻáᏠdata áááşáááŻá¸ ááááşá¸áááşá¸ááŹá¸áááŻááşáá˛áˇ container (ááąáášááŹ) áá áşáᯠááźá áşááŤáááşá
- Variable name: ááąáášááŹááąáŤáşá áĄáááş (age, name, salary)
- Variable value: ááąáášááŹáá˛á áĄáᏠ(25, "John", 5000.50)
- Data type: ááąáášáᏠáĄááťááŻá¸áĄá áŹá¸ (int, String, double)
ááŹááźáąáŹááˇáş áááŻáĄááşáá˛?
- Store data - Information ááááşá¸ááŹá¸áááŻááşáááş
- Reuse values - áá°ááŽáá˛áˇ value ááᯠááąááŹáĄááťáŹá¸ááźáŽá¸áážáŹ ááŻáśá¸áááŻáˇááááş
- Modify data - Value ááźáąáŹááşá¸áá˛áááŻááşáááş
- Readable code - Code ááŹá¸áááşááá˝ááşáááş
2. Variable Declaration (ááźáąááŹááźááşá¸)
Syntax: dataType variableName;
// Single declarations
int age;
String name;
double salary;
boolean isStudent;
// Multiple declarations
int x, y, z;
dataType- Variable áážáŹ ááááşá¸áááˇáş data áĄááťááŻá¸áĄá áŹá¸variableName- Variable áĄáááş (áááŻááşáááŻááşáá˝áąá¸áááŻááşáááş)- Declaration ááźáŽá¸áááş memory áážáŹ ááąááŹáááŤááźáŽ
- ááŤááąáááˇáş value ááážáááąá¸á፠(uninitialized)
3. Variable Initialization (Value ááąá¸ááźááşá¸)
Declaration + Initialization Together (Recommended)
int age = 25;
String name = "Alice";
double price = 99.99;
boolean isActive = true;
int a = 10, b = 20, c = 30;
Separate Steps
// Declare first
int score;
String city;
// Initialize later
score = 100;
city = "Yangon";
System.out.println("Score: " + score);
System.out.println("City: " + city);
â ď¸ Important Rules
- Local variables must be initialized before use
- Uninitialized variable ááŻáśá¸áááş compilation error ááąáŤáşááŤáááş
- Best practice: Declaration áá˛áˇ initialization ááᯠáá áşááźáááşáááşá¸ ááŻááşááŤ
// â Error
int x;
System.out.println(x); // Error!
// â
Correct
int y = 10;
System.out.println(y); // Works
4. Variable Assignment (Value ááźáąáŹááşá¸áá˛ááźááşá¸)
// Initial value
int score = 50;
System.out.println("Initial: " + score);
// Reassign new value
score = 75;
System.out.println("Updated: " + score);
// Update based on previous value
score = score + 10;
System.out.println("Final: " + score); // 85
=operator á right side value ááᯠleft side variable áážáŹ ááááşá¸áááş- Old value á ááťáąáŹááşáá˝áŹá¸ááźáŽá¸ new value ááźá áşáá˝áŹá¸áááş
- Self-assignment ááŻááşáááŻáˇ ááááş:
x = x + 1; - Data type á compatible ááźá áşáááŤáááş
int age = 25; // â
Correct
age = 30; // â
Correct
age = "Thirty"; // â Error! Type mismatch
5. Variable Naming Rules (áĄáááşááąá¸á ááşá¸ááťááşá¸ááťáŹá¸)
â Valid Naming Rules
- Start with letter, $ or _ - Digit áá˛áˇ áá áááŻááşááŤ
- Can contain letters, digits, $ and _
- Case-sensitive - age â Age â AGE
- No spaces - Use camelCase instead
- Cannot use Java keywords - int, class, public á ááŹáá˝áą áááŻáśá¸áááŻááşááŤ
// â
Valid names
int age;
int studentAge;
int student_age;
int _count;
int $price;
int age2;
// â Invalid names
int 2age; // Cannot start with digit
int student-age; // Cannot use hyphen
int my age; // Cannot have space
int class; // Java keyword
int public; // Java keyword
đ Naming Conventions (Best Practices)
- camelCase for variables: studentName, totalScore
- Descriptive names: age áááŻáˇááąá¸ááŤ, a áááŻáˇ áááąá¸ááŤáá˛áˇ
- Avoid abbreviations: number áááŻáˇááąá¸ááŤ, num áááŻáˇ áááşáááŻááşáááťáž áážáąáŹááşááŤ
- Start with lowercase: userName (not UserName)
- Constants in UPPER_CASE: MAX_VALUE, PI
// â Bad naming
int a = 25;
int x = "John";
int sd = 5000;
// â
Good naming
int studentAge = 25;
String studentName = "John";
double studentSalary = 5000;
6. Literals (áááşáááŻá¸ áĄááťááŻá¸áĄá áŹá¸ááťáŹá¸)
A literal is a fixed value written directly in the code.
6.1 Integer Literals
int decimal = 100; // Decimal (base 10)
int binary = 0b1010; // Binary (base 2) = 10
int octal = 0144; // Octal (base 8) = 100
int hex = 0x64; // Hexadecimal (base 16) = 100
long bigNumber = 1000000000L; // Long literal (L suffix)
- Decimal: ááŻáśáážááş ááááşá¸ - 10, 100, 255
- Binary:
0báá˛áˇ á áááş - 0b1010 = 10 - Octal:
0áá˛áˇ á áááş - 0144 = 100 - Hexadecimal:
0xáá˛áˇ á áááş - 0xFF = 255 - Long suffix: L or l (capital L áááŻááąáŹááşá¸áááş)
- Underscore: Readability áĄáá˝ááş _ ááŻáśá¸áááŻáˇááááş - 1_000_000
6.2 Floating-Point Literals
double price = 99.99; // double (default)
float rate = 5.5f; // float (f suffix required)
double scientific = 1.5e3; // Scientific notation = 1500.0
double negative = -3.14;
// Underscore for readability
double million = 1_000_000.50;
- Double: Default ááźá áşáááş (64-bit) - 3.14, 99.99
- Float: f or F suffix áááŻáááş (32-bit) - 5.5f
- Scientific notation: 1.5e3 = 1.5 Ă 10Âł = 1500
- Precision: double á float áááş accurate áááŻááťáŹá¸áááş
6.3 Character Literals
char letter = 'A';
char digit = '9';
char symbol = '@';
// Escape sequences
char newline = '\n';
char tab = '\t';
char quote = '\'';
char backslash = '\\';
// Unicode
char heart = '\u2665'; // âĽ
- Single quotes: 'A', 'B', '1' (double quotes áááŻááşááŤ)
- One character only: 'AB' ááááŤ, "AB" á String ááŤ
- Escape sequences: \n (newline), \t (tab), \' (quote)
- Unicode: \uXXXX format - \u0041 = 'A'
6.4 String Literals
String name = "John Doe";
String message = "Hello, World!";
String empty = ""; // Empty string
String number = "123"; // String, not number!
// Escape sequences in strings
String path = "C:\\Users\\Desktop";
String quote = "He said, \"Hello\"";
String multi = "Line 1\nLine 2\nLine 3";
- Double quotes: "Hello" (single quotes áááŻááşááŤ)
- Multiple characters: "", "A", "ABC", "Hello World"
- Escape sequences work: \n, \t, \", \\
- Immutable: String value ááźáąáŹááşá¸áááŻáˇááá፠(create new one)
- Concatenation: + operator áá˛áˇ ááąáŤááşá¸áááŻáˇ ááááş
6.5 Boolean Literals
boolean isStudent = true;
boolean isActive = false;
// Commonly used in conditions
boolean hasPermission = true;
boolean isComplete = false;
System.out.println(isStudent); // true
System.out.println(isActive); // false
- Only two values: true or false
- Lowercase only: True, FALSE, 1, 0 ááááŤ
- Used for logic: Conditions, loops, flags
- Default value: false (for class-level variables)
7. Complete Example
public class VariableDemo {
public static void main(String[] args) {
// Integer variables
int studentCount = 30;
int totalMarks = 2850;
// Floating-point
double averageScore = 85.5;
double percentage = 95.67;
// Character
char grade = 'A';
// String
String courseName = "Java Programming";
String instructor = "Mr. Smith";
// Boolean
boolean isPassed = true;
boolean isCompleted = false;
// Display information
System.out.println("=== Course Information ===");
System.out.println("Course: " + courseName);
System.out.println("Instructor: " + instructor);
System.out.println("Students: " + studentCount);
System.out.println("Average Score: " + averageScore);
System.out.println("Percentage: " + percentage + "%");
System.out.println("Grade: " + grade);
System.out.println("Passed: " + isPassed);
System.out.println("Completed: " + isCompleted);
// Calculate
double scorePerStudent = (double) totalMarks / studentCount;
System.out.println("Score per student: " + scorePerStudent);
}
}
=== Course Information ===
Course: Java Programming
Instructor: Mr. Smith
Students: 30
Average Score: 85.5
Percentage: 95.67%
Grade: A
Passed: true
Completed: false
Score per student: 95.0
8. Key Takeaways
- Variables store data - Container for values
- Declaration: dataType variableName;
- Initialization: Give it a value
- Assignment: Change the value using =
- Naming rules: Start with letter/$/_, no spaces, no keywords
- camelCase convention: studentName, totalScore
- Integer literals: 100, 0b1010, 0x64, 1000L
- Float literals: 99.99, 5.5f, 1.5e3
- Char literals: 'A', '\n', '\u2665'
- String literals: "Hello", "Java"
- Boolean literals: true, false
- Local variables must be initialized before use
đĄ Practice Exercise
Create a StudentInfo program:
- Declare variables: name, age, grade, gpa, isEnrolled
- Initialize with appropriate values
- Print all information
- Calculate and print years until graduation (assume 4 year program)
đĄ Quick Quiz
Q1: Which is a valid variable name?
Q2: What's the correct way to declare and initialize an integer?
Q3: Which literal is a character?