1. Data Types ဆိုတာ ဘာလဲ?
A data type specifies what kind of value a variable can hold and how much memory it needs.
Data type က variable တစ်ခုမှာ ဘယ် အမျိုးအစား value သိမ်းလို့ရလဲ၊ memory ဘယ်လောက် လိုအပ်လဲ ဆိုတာ သတ်မှတ်ပေးပါတယ်။
- Type safety: မှားယွင်းတဲ့ data မသိမ်းအောင် ကာကွယ်ပေးတယ်
- Memory allocation: ဘယ်လောက် memory လိုအပ်လဲ သိနိုင်တယ်
- Operations: ဘယ် operations တွေ လုပ်လို့ရလဲ သတ်မှတ်ပေးတယ်
Example: int age = 25; - int က numbers သာ သိမ်းနိုင်တယ်၊ text
မသိမ်းနိုင်ပါ။
Java Data Types
│
├── Primitive Types (8 types)
│ ├── Integer Types: byte, short, int, long
│ ├── Floating-point: float, double
│ ├── Character: char
│ └── Boolean: boolean
│
└── Reference Types
├── String
├── Arrays
├── Classes
└── Interfaces
Primitive Types
8 basic types built into Java
Reference Types
Objects, arrays, strings
Memory Size
Each type uses specific memory
2. Primitive Data Types (အခြေခံ အမျိုးအစား ၈ ခု)
Java has 8 primitive data types that store simple values directly in memory.
2.1 Integer Types (ကိန်းပြည့် အမျိုးအစားများ)
byte (8-bit)
- Size: 1 byte (8 bits)
- Range: -128 to 127
- Default: 0
- Use: Save memory in large arrays
byte age = 25;
byte temperature = -10;
byte maxValue = 127;
byte minValue = -128;
short (16-bit)
- Size: 2 bytes (16 bits)
- Range: -32,768 to 32,767
- Default: 0
- Use: Save memory when int is too large
short year = 2024;
short population = 32000;
short maxShort = 32767;
int (32-bit) - Most Common
- Size: 4 bytes (32 bits)
- Range: -2,147,483,648 to 2,147,483,647
- Default: 0
- Use: Most common integer type
int studentCount = 100;
int salary = 50000;
int maxInt = 2147483647;
int minInt = -2147483648;
long (64-bit)
- Size: 8 bytes (64 bits)
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Default: 0L
- Use: Very large numbers
- Suffix: L or l (use L)
long worldPopulation = 8000000000L;
long distance = 384400000L; // Earth to Moon (km)
long bigNumber = 9223372036854775807L;
| Type | Size | Range | Use Case |
|---|---|---|---|
byte |
1 byte | -128 to 127 | အရမ်းသေးတဲ့ ကိန်း |
short |
2 bytes | -32K to 32K | သေးတဲ့ ကိန်း |
int |
4 bytes | -2B to 2B | အသုံးအများဆုံး ⭐ |
long |
8 bytes | -9Q to 9Q | အရမ်းကြီးတဲ့ ကိန်း |
📌 Tip: အများအားဖြင့် int သုံးပါ။
အရမ်းကြီးမှ long သုံးပါ။
2.2 Floating-Point Types (ဒသမ ကိန်းများ)
float (32-bit)
- Size: 4 bytes (32 bits)
- Precision: 6-7 decimal digits
- Default: 0.0f
- Suffix: f or F (required)
- Use: Save memory with large arrays of decimals
float price = 99.99f;
float pi = 3.14159f;
float temperature = 36.5f;
float rate = 5.5f;
double (64-bit) - Recommended
- Size: 8 bytes (64 bits)
- Precision: 15-16 decimal digits
- Default: 0.0d
- Suffix: d or D (optional)
- Use: Default for decimal numbers
double salary = 5000.50;
double pi = 3.141592653589793;
double scientific = 1.5e10; // 15000000000.0
double precise = 123.456789012345;
- float: 6-7 digits accurate, f suffix လိုတယ်, memory သက်သာတယ်
- double: 15-16 digits accurate, default ဖြစ်တယ်, precise ပိုများတယ်
- Scientific notation: 1.5e10 = 1.5 × 10¹⁰ = 15,000,000,000
- Precision matters: Financial calculations မှာ double သုံးပါ
// ❌ float - Less precise
float f = 123456789.0f;
System.out.println(f); // 1.23456792E8 (rounded!)
// ✅ double - More precise
double d = 123456789.0;
System.out.println(d); // 1.23456789E8 (accurate)
2.3 Character Type
char (16-bit Unicode)
- Size: 2 bytes (16 bits)
- Range: 0 to 65,535 (Unicode characters)
- Default: '\u0000'
- Use: Single character
- Notation: Single quotes 'A'
char letter = 'A';
char digit = '5';
char symbol = '@';
char unicode = '\u0041'; // 'A'
char newline = '\n';
char tab = '\t';
- Single character only: 'A' သုံးပါ, "A" မသုံးပါနဲ့
- Unicode support: Myanmar, Chinese, Emoji အားလုံး သုံးလို့ရတယ်
- Escape sequences: \n, \t, \', \", \\
- Actually a number: char က internally 0-65535 number ပါ
char burmese = 'က';
char emoji = '😀'; // Unicode emoji
char a = 'A';
System.out.println((int) a); // 65 (ASCII value)
2.4 Boolean Type
boolean
- Size: Not precisely defined (typically 1 bit)
- Values: true or false only
- Default: false
- Use: Logical conditions, flags
boolean isStudent = true;
boolean hasPermission = false;
boolean isComplete = true;
boolean isValid = false;
// Commonly used in conditions
if (isStudent) {
System.out.println("Student discount applied");
}
- Two values: true, false သာ ရှိတယ် (1, 0 မဟုတ်ပါ)
- Lowercase only: True, FALSE, TRUE မရပါ
- Logical operations: &&, ||, ! operators နဲ့ သုံးတယ်
- Conditions: if, while, for loops မှာ သုံးတယ်
3. Primitive Types Summary Table
| Type | Size | Range | Default | Example |
|---|---|---|---|---|
byte
|
1 byte | -128 to 127 | 0 |
byte b = 100;
|
short
|
2 bytes | -32,768 to 32,767 | 0 |
short s = 30000;
|
int ⭐
|
4 bytes | -2B to 2B | 0 |
int i = 100000;
|
long
|
8 bytes | -9Q to 9Q | 0L |
long l = 10000L;
|
float
|
4 bytes | 6-7 digits | 0.0f |
float f = 3.14f;
|
double
⭐ |
8 bytes | 15-16 digits | 0.0d |
double d = 3.14;
|
char
|
2 bytes | 0 to 65,535 | '\u0000' |
char c = 'A';
|
boolean
|
1 bit | true/false | false |
boolean b = true;
|
- int: အသုံးအများဆုံး integer type (age, count, index)
- double: အသုံးအများဆုံး decimal type (price, salary, average)
- boolean: Conditions, flags (isPassed, isActive)
- String: Text data (name, address, message)
4. Reference Data Types (Object များ)
Reference types store references (addresses) to objects in memory, not the actual values.
4.1 String Type
String name = "John Doe";
String message = "Hello, World!";
String empty = "";
String multiline = "Line 1\nLine 2";
// String methods
String upper = name.toUpperCase(); // "JOHN DOE"
int length = name.length(); // 8
char first = name.charAt(0); // 'J'
4.2 Arrays
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};
double[] prices = new double[10];
System.out.println(numbers[0]); // 1
System.out.println(names.length); // 3
- Primitive: Value တိုက်ရိုက် သိမ်းတယ် (int, double, boolean)
- Reference: Memory address သိမ်းတယ် (String, arrays, objects)
- Default: Primitives = 0/false, References = null
- Methods: Reference types မှာ methods ရှိတယ် (.length(), .toUpperCase())
- Mutability: Primitives immutable, some references mutable
5. Type Conversion (အမျိုးအစား ပြောင်းလဲခြင်း)
5.1 Implicit Conversion (Widening)
Automatic conversion from smaller to larger type.
// byte → short → int → long → float → double
byte b = 10;
int i = b; // Automatic (byte to int)
long l = i; // Automatic (int to long)
float f = l; // Automatic (long to float)
double d = f; // Automatic (float to double)
System.out.println(d); // 10.0
- Automatic: Java က အလိုအလျောက် လုပ်ပေးတယ်
- Safe: Data loss မရှိပါ
- Direction: သေးတာ → ကြီးတာ (byte → int → long → double)
- No cast needed: Casting မလိုပါ
5.2 Explicit Conversion (Narrowing)
Manual conversion from larger to smaller type (requires casting).
// Larger to smaller (need casting)
double d = 9.7;
int i = (int) d; // 9 (decimal part lost)
System.out.println(i);
long l = 100L;
int i2 = (int) l; // 100
byte b = (byte) i2; // 100
// Be careful with overflow!
int big = 130;
byte b2 = (byte) big; // -126 (overflow!)
- Manual: Casting လုပ်ရပါမယ် -
(type) value - Risky: Data loss ဖြစ်နိုင်တယ်
- Direction: ကြီးတာ → သေးတာ (double → int → byte)
- Precision loss: Decimal ဆုံးရှုံးနိုင်တယ်
- Overflow: Range ကျော်ရင် unexpected results
// ⚠️ Data loss examples
double d = 9.9;
int i = (int) d; // 9 (0.9 lost)
int big = 130;
byte b = (byte) big; // -126 (overflow!)
6. Complete Example
public class DataTypeDemo {
public static void main(String[] args) {
// Integer types
byte age = 25;
short year = 2024;
int population = 1000000;
long worldPopulation = 8000000000L;
// Floating-point types
float price = 99.99f;
double salary = 5000.50;
// Character
char grade = 'A';
char symbol = '@';
// Boolean
boolean isStudent = true;
boolean hasLicense = false;
// String (reference type)
String name = "Alice";
String course = "Java Programming";
// Display all types
System.out.println("=== Integer Types ===");
System.out.println("byte: " + age);
System.out.println("short: " + year);
System.out.println("int: " + population);
System.out.println("long: " + worldPopulation);
System.out.println("\n=== Floating-Point ===");
System.out.println("float: " + price);
System.out.println("double: " + salary);
System.out.println("\n=== Character ===");
System.out.println("char: " + grade);
System.out.println("symbol: " + symbol);
System.out.println("\n=== Boolean ===");
System.out.println("isStudent: " + isStudent);
System.out.println("hasLicense: " + hasLicense);
System.out.println("\n=== String ===");
System.out.println("name: " + name);
System.out.println("course: " + course);
// Type conversion
System.out.println("\n=== Type Conversion ===");
int x = 100;
long y = x; // Widening
double z = y; // Widening
System.out.println("Widening: " + x + " → " + y + " → " + z);
double d = 9.7;
int i = (int) d; // Narrowing
System.out.println("Narrowing: " + d + " → " + i);
}
}
7. Key Takeaways
- 8 primitive types: byte, short, int, long, float, double, char, boolean
- int: Most common integer (4 bytes, -2B to 2B)
- double: Most common decimal (8 bytes, 15-16 digits)
- long suffix: 1000000L (L required)
- float suffix: 99.99f (f required)
- char: Single quotes 'A' (2 bytes Unicode)
- boolean: true or false only
- String: Reference type for text (double quotes)
- Widening: Automatic (byte → int → long → double)
- Narrowing: Manual casting (double → int → byte)
- Default values: 0, 0.0, false, null
- Choose right type: Memory vs Range trade-off
💡 Practice Exercise
Create a StudentRecord program with:
- Use all 8 primitive types appropriately
- Include student age (byte), year (short), ID (int), phone (long)
- GPA (float), percentage (double), grade (char), passed (boolean)
- Name and course (String)
- Demonstrate type conversion
💡 Quick Quiz
Q1: Which data type is best for storing salary?
Q2: What is the size of int in Java?
Q3: Which requires explicit casting?