Primitive Type සහ Reference Type කියන්නේ මොනවද?

Hi, I’m Harsha Fernando. I love coding, learning new tech, and sharing knowledge in Sinhala. I write about programming, web development, and real-world projects.
Programming language වල Data Store කරන විදි දෙකක් තියෙනවා:
Primitive Types
Reference Types (Non-Primitive Types)
මේ දෙක අතර මූලික වෙනස තියෙන්නේ Memory එකේ Data තියෙන විදිහ.
Primitive Types කියන්නේ මොනවද?
Primitive type එකක් කියන්නේ simple values අපි direct store කරන data type එකක්.
Value එකම memory එකේ store කරනවා
Address එකක් වෙනම handle කරන්නෙ නෑ
Fast
Fixed size
Java වල Primitive Types
| Type | Use කරන දෙය |
int | පූර්ණ සංඛ්යා |
double | Decimal සංඛ්යා |
float | Decimal |
char | එක අකුරක් |
boolean | true / false |
byte, short, long | වෙනත් සංඛ්යා types |
Memory Concept (Primitive)
int x = 10;
Memory එකේ:
x → 10
Value එක direct තියෙන්නේ x variable එක ඇතුළේ.
Code Example
int a = 5;
int b = a;
b = 20;
System.out.println(a); // 5
System.out.println(b); // 20
🔎 ඇයි a change වෙලා නැත්තේ?
👉 b = a කියද්දී value copy වෙනවා, ඒ කියන්නෙ a ගේ value එක b ට copy වෙනවා තවත් කිව්වොත් මේකට අනුව b = 5 යි.
👉 a සහ b දෙක වෙන වෙනම memory locations වල තියෙන්නේ ඒකයි b වෙනස් උනත් a වෙනස් උනේ නැත්තේ.
Reference Types කියන්නේ මොනවද?
Reference type එකක් කියන්නේ actual data එක memory එකේ වෙනම තැනක (Heap) store කරලා, variable එකේ ඒ memory address එක store කරන එකට.
Variable එකේ value එක නෙමෙයි,
Object එකට යන address එක තමයි store කරන්නේ.
Objects, Arrays, Strings — මේවා Reference Types
Memory Concept (Reference)
Person p = new Person();
Memory:
p → (address) → Person Object
pvariable එකේ store කරලා තියෙන්නේ අපි object එක නෙමෙයි මේ තියෙන්නෙ ඒ object එකට යන reference (address) එක.
Code Example
class Person {
String name;
}
Person p1 = new Person();
p1.name = "Kamal";
Person p2 = p1; // reference copy
p2.name = "Nimal";
System.out.println(p1.name); // Nimal
System.out.println(p2.name); // Nimal
🔎 ඇයි දෙකම change වුණේ?
හරි දැන් මේක තමයි හොදටම තේරුම් ගන්න ඕන තැන. දැන් බලන්න ඔන්න අපි p2 එකට p1 එකේ memory address එක store කරා, ඒ කිව්වේ? ඒ කියන්නෙ දැන් 2න්නගෙම address එක එකමයි. ඉතින් ඔන්න අපි මොකක්ද කරේ, ඒ adrees එකට ගිහින් එකේ තියෙන name එක වෙනස් කරා “Kamal” තිබ්බ එක “Nimal” විදියට, ඉතින් එහෙම කරාම අපි මොන විදියට එක එක variables වලින් ගිහින් ඇහුවත් එකේ දැන් තියෙන නම “Kamal” නෙමෙයි “Nimal”. මේක මෙතනදී Primitive වල වගේ Value copy වෙලා නෑ, Reference copy වෙලා තියෙනනේ. මන් හිතනවා දැන් පැහැදිලි ඇති කියලා.
Primitive vs Reference - ප්රදාන වෙනස්කම්
| Feature | Primitive Type | Reference Type |
| Store කරන දෙය | Value එක | Address එක |
| Memory Location | Stack | Heap (object) |
| Copy වෙද්දී | Value copy | Address copy |
| Speed | Fast | ටිකක් slow |
| Null value | ❌ (default values තියෙනවා) | ✅ Null වෙන්න පුළුවන් |
| Example | int, double, boolean | String, Array, Object |
Simple Real-World Example
Primitive = ඔයා pocket එකේ cash තියාගන්නවා
Reference = ඔයා bank account number එක තියාගන්නවා (money තියෙන්නේ bank එකේ)
Quick Summary
✔ Primitive Type
\= Variable එකේම value එක තියෙනවා
✔ Reference Type
\= Variable එකේ තියෙන්නේ value (object) එකට යන address එක
ඔයා Java backend පටන් ගන්නවනම් මේ concept එක හොඳට තේරුම් ගන්නම ඕන
OOP, Objects, Spring Boot ඔක්කොම මේක මත build වෙන්නේ.





