PROPERTIES OF TYPES AND OBJECTS
Set of Values
Programming Language-ல் Type என்பது ஒரு data item எந்த வகையான value-ஐ வைத்திருக்கலாம், அதற்கு என்ன operations செய்யலாம் என்பதைக் குறிப்பிடுகிறது. Object என்பது program execution-ல் memory-ல் உருவாக்கப்படும் ஒரு data entity. Simple idea: Ty…
Programming Language-ல் Type என்பது ஒரு data item எந்த வகையான value-ஐ வைத்திருக்கலாம், அதற்கு என்ன operations செய்யலாம் என்பதைக் குறிப்பிடுகிறது.
Object என்பது program execution-ல் memory-ல் உருவாக்கப்படும் ஒரு data entity.
Type = Rule / Category Object = அந்த type-ஐ சேர்ந்த actual data stored in memory
int age = 25;
- int → Type
- age → Object / Variable
- 25 → Value
- age-க்கு memory location இருக்கும்
ஒரு real-life example எடுத்துக்கொள்வோம்.
Type = "Car" என்ற category
- wheels இருக்கும்
- engine இருக்கும்
- speed இருக்கலாம்
- start/stop செய்யலாம்
ஆனால்
Object = ஒரு particular car
- Car Type
- +--- Car 1+--- Car 2+--- Car 3
Programming-லும் அதே concept.
int a = 10; int b = 20;
இங்கு:
1Type = int
- Objects:
- a
- b
a, b இரண்டும் different objects. ஆனால் இரண்டின் type ஒன்றே.
- Data
- Type is selected
- Object is created
- Memory is allocated
- Value is stored
- Operations are performed
- Object lifetime ends
- Memory is released
int x = 10;
- int
- x object created
- memory allocated
- 10 stored
- x can participate in integer operations
- WHAT IS A TYPE?
Type என்பது ஒரு value-ன் nature and behaviour-ஐ define செய்கிறது.
int x = 10;
float y = 10.5;
char ch = 'A';
bool result = true;
இங்கு:
| Object | Type | Value |
|---|---|---|
| x | int | 10 |
| y | float | 10.5 |
| ch | char | 'A' |
| result | bool | true |
- என்ன values store செய்யலாம்
- எவ்வளவு memory தேவை
- எந்த operations allowed
- values எப்படி represent செய்யப்படும்
- type checking எப்படி நடக்கும்
Set of Values
ஒவ்வொரு type-க்கும் அது represent செய்யக்கூடிய possible values இருக்கும்.
bool x;
true
false
- Type
- defines
- Set of possible values
bool → true, false
char → 'A', 'B', '1', '#', ...
int → ..., -2, -1, 0, 1, 2, ...
Set of Operations
ஒரு type-க்கு எந்த operations செய்யலாம் என்பதையும் type determine செய்கிறது.
int a = 10; int b = 5;
1cout << a + b;
போன்ற arithmetic operations possible.
+
-
*
/
%
string a = "Hello";
string b = "World";
1cout << a + b;
இங்கு + என்பது string concatenation.
அதனால் operation-ன் behaviour கூட type-ஐ பொறுத்தது.
Representation
ஒரு value computer memory-ல் எப்படி represent செய்யப்படுகிறது என்பதையும் type specify செய்யும்.
int x = 10;
Computer நேரடியாக decimal 10 store செய்யாது.
Binary representation பயன்படுத்தும்.
- 10
- 1010
- Type
- determines
- Internal representation
Size
ஒரு type எவ்வளவு memory பயன்படுத்தும் என்பது முக்கிய property.
C++ example:
1cout << sizeof(int); 2cout << sizeof(char); 3cout << sizeof(double);
y = 10
Exact size implementation/platform-ஐ பொறுத்து மாறலாம்.
- char → 1 byte
- int → commonly 4 bytes
- double → commonly 8 bytes
C++ standard எல்லா machines-லும் int = exactly 4 bytes என்று guarantee செய்யாது.
Range
Type எந்த minimum மற்றும் maximum values-ஐ represent செய்ய முடியும் என்பதை range என்கிறோம்.
- Example concept
Type
↓
Size
↓
Number of bits
↓
Possible values
↓
Range
- ஒரு 8-bit unsigned value எடுத்தால்
00000000 → 0
11111111 → 255
- Range
0 to 255
Precision
Floating-point types-ல் precision முக்கியமான property.
float x = 3.1415926;
double y = 3.141592653589793;
float → lower precision
double → higher precision
ஒரு numeric value எவ்வளவு accurate-ஆக represent செய்யப்படுகிறது.
Type Compatibility
இரண்டு types ஒன்றுடன் ஒன்று operation செய்ய அல்லது assign செய்ய compatible-ஆக இருக்கிறதா என்பது type compatibility.
int x = 10; double y = x;
இங்கு integer value double-ஆக convert செய்யப்படுகிறது.
- int
- ↓ conversion
- double
ஆனால் எல்லா types-க்கும் direct compatibility இருக்காது.
Type Conversion
ஒரு type-லிருந்து மற்றொரு type-க்கு value மாற்றப்படுவது type conversion.
- Type Conversion
- +--- Implicit
- +--- ExplicitImplicit Conversion
Compiler automatically conversion செய்கிறது.
int x = 10; double y = x;
- 10int
- automatic conversion
- 10.0doubleExplicit Conversion
Programmer conversion-ஐ explicitly குறிப்பிடுகிறார்.
double x = 10.8; int y = static_cast<int>(x);
y = 10
Fractional part lost.
Type Checking
Program-ல் பயன்படுத்தப்படும் types valid-ஆக உள்ளதா என்று checking செய்வது type checking.
int x = 10;
x integer object.
integer + completely incompatible object
Programming language/compiler இதை check செய்யும்.
Static Type Checking
Compile time-ல் type checking நடக்கிறது.
int x = 10;
Compiler x-ன் type-ஐ compile time-லேயே அறிந்திருக்கும்.
- Source code
- Compiler
- Type checking
- Execution
C, C++, Java போன்ற languages பெரும்பாலும் statically typed.
Dynamic Type Checking
Runtime-ல் type information check செய்யப்படுகிறது.
Python example:
1x = 10 2x = "Python"
ஒரே name different times-ல் different type objects-ஐ refer செய்யலாம்.
x → int object
later
x → string object
Python dynamically typed language.
Type Equivalence
இரண்டு types same type-ஆக consider செய்யப்படுகிறதா என்பதே type equivalence.
- Type Equivalence
- +--- Name Equivalence
- +--- Structural EquivalenceName Equivalence
இரண்டு types same declared type name கொண்டிருந்தால் equivalent.
Type A
Type B
இரண்டின் structure same இருந்தாலும் names different என்றால் separate types என்று consider செய்யலாம்.
Structural Equivalence
Type names முக்கியமில்லை.
அவற்றின் structure same என்றால் equivalent என்று consider செய்யலாம்.
- integer
- character
- integer
- character
- Structure same.
- அதனால் structural equivalence approach-ல் equivalent ஆகலாம்.
OBJECT
Object என்பது program execution-ன் போது ஒரு value-ஐ hold செய்யக்கூடிய storage-associated entity.
Memory-ல் இருக்கும் actual data entity தான் Object.
int marks = 90;
இங்கு:
1Type = int 2Object = marks 3Value = 90 4OBJECT ≠ OOP OBJECT ONLY
இந்த point மிகவும் முக்கியம்.
"Object" என்ற word கேட்டவுடன் C++ class object மட்டும் நினைக்கக்கூடாது.
int x = 10;
x கூட ஒரு data object என்று broadly குறிப்பிடப்படலாம்.
Student s;
இங்கு s ஒரு class object.
அதனால் context பார்க்க வேண்டும்.
ஒவ்வொன்றாக பார்க்கலாம்.
- Object
- +-- Name+-- Type+-- Value+-- Address / Location+-- Size+-- Lifetime+-- Scope / Visibility+-- Mutability+-- Storage duration
NAME
Object-ஐ program-ல் identify செய்ய பயன்படும் identifier தான் name.
int salary = 50000;
இங்கு:
1salary = object name
Programmer salary என்ற name மூலம் அந்த object-ஐ access செய்கிறார்.
TYPE
ஒவ்வொரு object-க்கும் ஒரு type இருக்கும்.
1int age = 25; 2Object = age 3Type = int
- object என்ன values வைத்திருக்கலாம்
- என்ன operations செய்யலாம்
- memory representation எப்படி இருக்கும்
VALUE
Object தற்போது வைத்திருக்கும் information தான் value.
1int x = 10; 2Object = x 3Value = 10
1x = 20;
1Object = same x 2Value = 20
இதனால் object மற்றும் value ஒன்று அல்ல என்பதை புரிந்துகொள்ள வேண்டும்.
VERY IMPORTANT
int x = 10;
1x = 20;
x object மாறவில்லை.
அதன் value மட்டும் மாறியது.
x → 10
x → 20
MEMORY LOCATION / ADDRESS
Object பொதுவாக memory-ல் ஒரு location-ஐ occupy செய்கிறது.
int x = 10;
Address Value
1000 10
x அந்த memory location-ஐ identify செய்கிறது.
C++:
1cout << &x;
&x object x-ன் address-ஐ பெறுகிறது.
SIZE
ஒரு object memory-ல் எவ்வளவு space பயன்படுத்துகிறது என்பதே size.
int x = 10;
1cout << sizeof(x);
- x
- type = int
- int representation requirement
- object size
LIFETIME
ஒரு object memory-ல் எவ்வளவு நேரம் exist செய்கிறது என்பதே அதன் lifetime.
முக்கியமான concept.
- Object created
- Object exists
- Object used
- Object destroyed
இந்த entire period:
Object Lifetime
1void test() 2{ 3 int x = 10; 4}
அதனால் x-ன் lifetime function execution duration-க்கு உட்பட்டது.
- test() starts
- x created
- x exists
- test() ends
- x destroyed
SCOPE
Scope என்பது object/name program-ன் எந்த பகுதியில் accessible என்பதைக் குறிப்பிடுகிறது.
1void test() 2{ 3 int x = 10; 4 cout << x; 5}
x function block-க்குள் accessible.
Outside:
1cout << x;
பொதுவாக அந்த local declaration visible இல்லாததால் access செய்ய முடியாது.
SCOPE vs LIFETIME
மிகவும் முக்கியமான TRB concept.
- Scope
- Where can the name be used?
- Lifetime
- How long does the object exist?
Scope = எங்கே access செய்யலாம்?
Lifetime = எவ்வளவு காலம் memory-ல் இருக்கும்?
MUTABILITY
Object-ன் value change செய்ய முடியுமா என்பதை mutability குறிக்கிறது.
1int x = 10; 2x = 20;
Value change செய்ய முடிகிறது.
Conceptually mutable.
Constant Object
const int x = 10;
பின்னர்:
1x = 20;
allowed இல்லை.
Mutable Object
→ value can change
Immutable/constant object
→ value cannot be changed through that object
STORAGE DURATION
Object memory-ல் எந்த விதத்தில், எவ்வளவு காலம் storage பெறுகிறது என்பதுடன் தொடர்புடைய concept.
- Automatic
- Static
- Thread
- Dynamic
- Automatic Storage
- Static Storage
- Dynamic Storage
- Automatic Object
1void fun() 2{ 3 int x = 10; 4}
x block enter செய்யும்போது created.
Block exit செய்யும்போது destroyed.
1Enter block 2 ↓ 3x created 4 ↓ 5use x 6 ↓ 7exit block 8 ↓ 9x destroyed 10Static Object 11void fun() 12{ 13 static int count = 0; 14 count++; 15}
count function call முடிந்த பிறகும் அதன் value preserve ஆகும்.
fun(); fun(); fun();
- Call 1 → 1
- Call 2 → 2
- Call 3 → 3
Object storage program execution முழுவதும் நீடிக்கக்கூடும்.
Dynamic Object
Program execution time-ல் explicitly allocate செய்யப்படும் object.
int *p = new int(10);
- new
- memory allocated
- integer object created
- p points to object
Later:
delete p;
Memory release செய்ய வேண்டும்.
BINDING
ஒரு object-ன் property ஒரு entity-க்கு associate செய்யப்படுவதை binding என்கிறோம்.
int x = 10;
x → int
என்பது name-to-type binding.
x → memory location
என்பது name-to-location binding.
x → 10
என்பது current value association.
TYPE AND OBJECT RELATIONSHIP
int number = 50;
- int
- Type
- number
- Object / Variable name
- 50
- Value
- Memory
- Storage location
- Type
- defines properties
- Object
- occupies storage
- holds ValueTYPE vs VALUE vs OBJECT
இந்த மூன்றும் confuse ஆகக்கூடாது.
int x = 100;
| Concept | Answer |
|---|---|
| Type | int |
| Object | x |
| Value | 100 |
| Name | x |
| Memory | storage occupied by x |
VARIABLE vs OBJECT
int x = 10;
x-ஐ variable என்றும் சொல்லலாம்.
Broader programming language terminology-ல் அது ஒரு object/data object ஆகவும் பார்க்கப்படும்.
Variable
Value change செய்யக்கூடிய named entity என்ற basic idea.
Object
Storage மற்றும் value-ஐ represent செய்யும் broader concept.
TYPE PROPERTY SUMMARY
- TYPE
- +-- Set of values
- +-- Allowed operations
- +-- Representation
- +-- Size
- +-- Range
- +-- Precision
- +-- Compatibility
- +-- Conversion rules
- +-- Type checking
- +-- Type equivalenceOBJECT PROPERTY SUMMARYOBJECT
- +-- Name
- +-- Type
- +-- Value
- +-- Memory location
- +-- Size
- +-- Scope
- +-- Lifetime
- +-- Mutability
- +-- Storage duration
| Type | Object |
|---|---|
| Classification | Actual entity |
| Defines valid values | Holds a value |
| Defines operations | Operations are performed on it |
| Determines representation | Occupies/uses storage according to representation |
| Example: int | Example: x |
| Conceptual description | Runtime/data entity |
int x = 10; int y = 20;
Type:
int
- Objects:
- x
- y
Values:
10
20
int is an object
Incorrect in this context.
int is a type.
10 is the variable
Incorrect.
10 = value
Scope and lifetime are same
Incorrect.
- Scope
- visibility of name
- Lifetime
- existence duration of object
Every object means only class object.
Incorrect.
Programming language theory-ல் data object என்ற broader meaning உண்டு.
TOV
T → Type
O → Object
V → Value
1int age = 25; 2T = int 3O = age 4V = 25
- NTVASLM
- N → Name
- T → Type
- V → Value
- A → Address
- S → Size / Scope
- L → Lifetime
- M → Mutability
Type determines the set of values and operations.
Object is an actual program entity that can hold a value.
One type can have many objects.
An object's value may change while its identity/storage entity remains.
Scope and lifetime are different concepts.
Type checking may occur at compile time or runtime.
Type equivalence can be based on name or structure.
Object properties include type, value, storage, scope and lifetime.
- TRB/NET/SET exam-க்கு மிகவும் முக்கியமான statements
Question: A data type defines what?
- Answer
A set of values together with the operations permitted on those values.
- Question
Scope of a variable refers to?
- Answer
The region of program in which the name is visible/accesssible.
- Question
Lifetime of an object refers to?
- Answer
The period during program execution for which the object exists.
- Question
Type checking performed before execution is?
- Answer
Static type checking.
- Question
Type checking during execution is?
- Answer
Dynamic type checking.
Consider:
int x = 10;
"What are the type, object and value?"
Type = int Object = x Value = 10
x = 50;
Object = x remains the same Value = changes from 10 to 50
For the following statement identify Type, Object and Value:
double salary = 45000.50;
Answer:
Type = double Object = salary Value = 45000.50
Now:
char grade = 'A';
Answer:
Type = char Object = grade Value = 'A'
- TYPE
- ↓
- defines
- ↓
- Values + Operations +Representation
- ↓
- OBJECT
- ↓
- has
- ↓
- Name + Type + Value + Address
- ↓
- alsohas
- ↓
- Scope + Lifetime + Storage +Mutability
- ஒரே line-ல் நினைவில் வைக்க
- Type என்பது data-க்கு rule book;Object என்பது அந்த rules-ஐ பின்பற்றி program memory-ல் இருக்கும் actual entity.
- Next syllabus connection
- Properties of Types and Objects →Scalar Data Types → Integer, Floating Point, Character, Boolean, Enumeration → Composite Data Types.