data types.pdf
Document Details
Uploaded by Deleted User
Full Transcript
DATA TYPES PRINCIPLES OF PROGRAMMING LANGUAGE INTRODUCTION Each programming language contains constructs and mechanisms for structuring data. Instead of just the simple sequences of bits in the physical machine, a high- level language provides complex, structured data which more easily lends itself...
DATA TYPES PRINCIPLES OF PROGRAMMING LANGUAGE INTRODUCTION Each programming language contains constructs and mechanisms for structuring data. Instead of just the simple sequences of bits in the physical machine, a high- level language provides complex, structured data which more easily lends itself to describing the structure of the problems that are to be solved. These constructs and mechanisms are formed from what is called the type system of a language. Variables in programming are fundamental elements that allow developers to store and manipulate data. The concepts of declaration, binding, and scoping are crucial for understanding how variables function within a program. Variable Declaration The process of defining a variable by specifying its name and type, which allows the program to allocate memory for storing values. Purpose Memory Allocation Data Type Specification Code Readability Scope Definition Variable Binding The association between a variable name and its value or memory location. This occurs when a value is assigned to the declared variable. Example int age = 25 char start = “a” Variable Scope defines the visibility and lifetime of a variable within a program. It determines where a variable can be accessed or modified. to control their accessibility within different parts of a program Local Scope: Variables declared within a function or block are accessible only within that function or block. Global Scope: Variables declared outside of any function are accessible throughout the entire program. Questions? DATA TYPES A data type is a homogeneous collection of values, effectively presented, equipped with a set of operations which manipulate these values. A “type” is a collection of values “Homogeneous”, these values must share any structural property which makes them similar to each other. “equipped with” the operations which manipulate them. “effectively presented”, which refers to values. a group of similar values that share common traits, come with operations to work with them, and are clearly defined by the values they represent. DATA TYPES Data types are present in programming languages for at least three different reasons: 1. At the design level, as support for the conceptual organisation; 2. At the program level, as support for correctness; 3. At the translation level, as support for the implementation. Classification of Data types 1. Scalar Types 2. Composite Types Scalar Types SCALAR TYPES Scalar (or simple) types are those types whose values are not composed of aggregations of other values. Boolean Complex Character Void Integers Enumerations Reals Intervals Fixed Point Ordered Types SCALAR TYPES Boolean Values: The two truth values, true and false. Operations: an appropriate selection from the main logical operations: conjunction (and), disjunction (or) and negation (not), equality, exclusive or, etc. Character Values: a set of character codes, fixed when the language is defined; the most common of these are ASCII and UNICODE. Operations: strongly dependent upon the language; we always find equality, comparison and some way to move from a character to its successor and/or to its predecessor. SCALAR TYPES Integer Values: A finite subset of the integers, usually fixed when the language is defined. Operations: the comparisons and an appropriate selection of the main arithmetic operators (addition, subtraction„ multiplication, integer division, remainder after division, exponentiation, etc.). Common integer types: int, short, long, unsigned int Examples: 25, -5, 189992200, 42. SCALAR TYPES Real Values: an appropriate subset of the rational numbers, usually fixed when the language is defined; the structure of such a subset depends on the representation adopted. Operations: comparisons and an appropriate selection of the main numeric operations (addition, subtraction, multiplication, division, exponentiation, square roots, etc.). Common Real types: float, double, long double Examples: 3.14, 2.718281828459, 1.618033988749895 SCALAR TYPES Fixed Point Values: an appropriate subset of the rational numbers, usually fixed when the language is defined; the structure of such a subset depends on the representation adopted. Operations: Numbers where the decimal point position is fixed, used in scenarios where precision is important, like in financial calculations. Example: 123.45 where two decimal places are always fixed for currency representation. SCALAR TYPES Complex Values: an appropriate subset of the complex numbers, usually fixed by the definition of the language; the structureof this subset depends on the adopted representation. Operations: A number with a real and an imaginary part. Example: 3+4i3 + 4i3+4i, where 3 is the real part and 4i is the imaginary part. SCALAR TYPES Void Represents the absence of any value, commonly used in programming functions that don't return a value. Values: only one, which can be written as ( ). Operations: none. Enumeration An enumeration type consists of a fixed set of constants, each characterized by its own name. Example type Dwarf = {Bashful, Doc, Dopey, Grumpy, Happy, Sleepy, Sneezy}; SCALAR TYPES Intervals form a contiguous subset of the values of another scalar type. Example: Interval [1, 10] represents all numbers between 1 and 10, including 1 and 10. Ordered Types The boolean, character, integer, enumeration and interval types are examples of or- dered types (or discrete types) Values that can be compared and arranged in a sequence (often numerical or alphabetic). Example: Integers (1, 2, 3,...) or Characters ('A', 'B', 'C',...). Questions? Composite Types COMPOSITE TYPES obtained by combining other types using appropriate constructors. The most important and common composite types are: Record (or structure), an collection of values in general of different type. Array (or vector), a collection of values of the same type. Set: subsets of a base type, generally ordinal types. Pointer: 1-values which permit access to data of another type. Recursive types: types defined by recursion, using constants and constructors; particular cases of recursive types are lists, trees, etc. COMPOSITE TYPES Record (or structure) a collection formed from a finite number of (in general ordered) elements called fields, which are distinguished by name. Example: type Student = struct { int year; float height; }; COMPOSITE TYPES Array (or vector) is a finite collection of elements of the same type, indexed by an interval of ordinal type From a semantic viewpoint, an array is a function which has, as domain, an interval for the indices and, as codomain, a type for the array elements. Example int W[21..30]; type Dwarf = {Bashful, Doc, Dopey, Grumpy, Happy, Sleepy, Sneezy}; float Z[Dopey..Sneezy]; COMPOSITE TYPES Set values are composed of subsets of a base type (or uni- verse), usually restricted to an ordinal type. A collection that contains unique, unordered elements with no duplicates. Example Set colors = new HashSet(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); COMPOSITE TYPES Pointer (or reference) permit the direct manipulation of l-values holding the address of an object in memory. Example class Person { Person person1 = new Person("Alice"); String name; Person person2 = person1; Person(String name) { person2.name = "Bob"; this.name = name; System.out.println(person1.name); } } COMPOSITE TYPES Recursive Type type can contain a value of the same type. A type that refers to itself in its own definition, commonly used in data structures like linked lists or trees. Example Questions? Data types in Java Memory Management and Storage Allocation Memory Management One of the functions of the interpreter associated with an abstract machine. This functionality manages the allocation of memory for programs and for data, that is determines how they must be arranged in memory, how much time they may remain and which auxiliary structures are required to fetch information from memory. The process of coordinating and handling computer memory, which includes allocating, using, and freeing memory resources. Memory Management Two primary strategies for memory management Static Memory Management performed by the compIler before execution starts. Typical elements for which it is possible statically to allocate memory are global variables Dynamic Memory Management performed during runtime after execution starts. This allows programs to request and release memory as needed during execution, making it flexible for varying data sizes. Comparison Static Memory Dynamic Memory Feature Management Management Allocation Time Compile-time Run-time Memory Size Fixed size Variable size Throughout program Lifetime Until explicitly freed execution Memory Area Used Stack (for local/static variables) Heap Faster due to compile-time Slower due to run-time Speed handling overhead Flexibility Less flexible Highly flexible Storage Allocation Storage Allocation Storage allocation refers to the method by which programs request and receive memory space for their data structures. It can occur in different areas of memory: 1. Stack allocation 2. Static Allocation 3. Heap Allocation Stack Allocation Memory is allocated in a last-in-first-out (LIFO) manner. Variables declared within functions are stored on the stack. Other stacking techniques are, FIFO, Random Access and Priority Queue. D LIFO- DCBA FIFO - ABCD C RA - CBDA (Randomized) B PQ - ABCD (Depending on the prioritization) A Static Allocation Refers to the process of reserving memory for variables and data structures at compile time. The size and location of the allocated memory are determined before the program starts running, making it fixed throughout the program's execution. Memory is allocated when the program is compiled, not during execution. The size of the allocated memory must be known in advance and cannot change during runtime. Heap Allocation The process of reserving a block of memory from the heap, which is a large area of memory available for dynamic allocation. Unlike stack memory, which is managed automatically and has a fixed size, heap memory can be allocated and deallocated at any time during program execution. Allows for dynamic data structures whose size can change during execution. STACK VS HEAP Summary Data Types are classifications that define the nature of data a variable can hold and determine how that data can be manipulated within a program. Understanding data types is crucial for effective programming, as they influence memory allocation, operations, and data handling. Scalar Data Types Boolean Complex Character Void Integers Enumerations Reals Intervals Fixed Point Ordered Types Summary Dynamic Data Types Record (or structure) Array (or vector) Set Pointer (Or reference) Recursive types Overall, scalar data types provide the basic units of data representation in programming, while dynamic data types offer flexibility for managing more complex structures. Feature Scalar Types Composite Types Built from multiple scalar types or Definition Hold a single value. other composite types. Cannot be decomposed into Can be decomposed into Decomposability simpler types. individual elements or members. - Arrays- Structures (struct)- Examples - int- float- bool- char- enum Unions- Classes Used for basic data Used for grouping related data Usage representation and arithmetic and creating complex data operations. structures. Memory size depends on the Typically occupy a fixed amount of Memory Allocation number of elements and their memory (e.g., 1, 2, 4, or 8 bytes). types. Summary Effective memory management encompasses understanding various allocation techniques—static vs. dynamic, stack vs. heap— and their implications on performance and resource utilization. Each method has its advantages and disadvantages, making it essential for developers to choose the appropriate strategy based on the specific needs of their applications. Mastering these concepts leads to more efficient, reliable, and maintainable code.