Primitive Data Types: Numbers Strings Ordinal Types Pointers

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

1 Introduction to Data Types (Section 7.1) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
ICE1341 Programming Languages Spring 2005 Lecture #9 Lecture #9 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
CS 355 – Programming Languages
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
ISBN Lecture 06 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Lecture 06 Topics Introduction Primitive Data.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Elementary Data Types Scalar Data Types Numerical Data Types Other
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Lecture 6 Concepts of Programming Languages
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 355 – Programming Languages
CS 330 Programming Languages 10 / 23 / 2008 Instructor: Michael Eckmann.
1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill.
College of Computer Science and Engineering
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
Names Variables Type Checking Strong Typing Type Compatibility 1.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
C H A P T E R S I X Data Types.
1 CS Programming Languages Class 09 September 21, 2000.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
CS 330 Programming Languages 10 / 31 / 2006 Boo! Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
1 Data Types Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer Types.
Data Types W E E K F O U R. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character String.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Chapter 6 Data Types.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Chapter 6: Data Types Lectures # 10.
Lecture 16: Introduction to Data Types
Structure of Programming Language
CS 326 Programming Languages, Concepts and Implementation
Instructor : Ahmed Alalawi Slides from Chung –Ta King
Concepts of programming languages
Chapter 6 Data Types.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Chapter 6 Data Types.
Introduction to Abstract Data Types
Chapter 6 Data Types.
Presentation transcript:

Primitive Data Types: Numbers Strings Ordinal Types Pointers Chapter 6 Primitive Data Types: Numbers Strings Ordinal Types Pointers

Data Types Data types provide a context for operations the + operator works differently for different types of numbers (and for strings)‏ Types also limit the operations that can be performed to those that make sense dividing strings or characters doesn't make sense Copyright © 2007 Addison-Wesley. All rights reserved.

Type Systems provide A mechanism for defining types and associating them with language constructs named constants, variables, record fields, parameters, … A set of rules for type equivalence type compatibility type inference Copyright © 2007 Addison-Wesley. All rights reserved.

Ways to think about types Denotational a type is a set of values Constructive types are built-in or created from existing types Abstraction-based a type is an interface consisting of a set of operations Copyright © 2007 Addison-Wesley. All rights reserved.

Definitions data type collection of data objects a set of predefined operations descriptor : collection of attributes for a variable data object : instance of a user-defined (abstract data) type Copyright © 2007 Addison-Wesley. All rights reserved. 5

Ways of classifying types Simple vs. compound Built-in vs. user-defined boolean, character, integer, floating point enumeration types, classes, records/structures discrete (enumerable) or not integer, boolean, character, enumeration, subrange real, compound Copyright © 2007 Addison-Wesley. All rights reserved.

Data Types Primitive Structured not defined in terms of other data types defined in the language often reflect the hardware Structured built out of other types Copyright © 2007 Addison-Wesley. All rights reserved. 7

Integer Types Usually use hardware representation May have several ranges Java’s signed integer sizes: byte, short, int, long C/C++ have unsigned versions of the same types Scripting languages often just have one integer type Python has an integer type and a long integer which can get as big as it needs to. python integer is same as C long (?= 32 bits)‏ Copyright © 2007 Addison-Wesley. All rights reserved. 8

Representing Integers Can convert positive integers to base 2 How do you handle negative numbers with only 0s and 1s? Sign bit Ones complement Twos complement - this is the one that is used Copyright © 2007 Addison-Wesley. All rights reserved. 9

Representing negative integers Sign bit Ones complement Copyright © 2007 Addison-Wesley. All rights reserved. 10

Twos Complement To get the binary representation, take the complement and add 1 Copyright © 2007 Addison-Wesley. All rights reserved. 11

Floating Point Types Model real numbers only an approximation due to round-off error For scientific use support at least two floating-point types (e.g., float and double; sometimes more Usually based on hardware IEEE Floating-Point Standard 754 32 and 64 bit standards Copyright © 2007 Addison-Wesley. All rights reserved. 12

Representing Real Numbers We can convert the decimal number to base 2 just as we did for integers How do we represent the decimal point? fixed number of bits for the whole and fractional parts severely limits the range of values we can represent Use a representation similar to scientific notation Copyright © 2007 Addison-Wesley. All rights reserved. 13

IEEE Floating Point Representation Normalize the number one non-zero bit before decimal point Use one bit to represent the sign (1 for negative)‏ Use a fixed number of bits for the exponent which is offset to allow for negative exponents Exponent = exponent + offset (-1)sign 1.Fraction x 2Exponent Copyright © 2007 Addison-Wesley. All rights reserved. 14

Floating Point Types C, C++ and Java have two floating point types double Most scripting languages have one floating point type Python's floating point type is equivalent to a C double Some scripting languages only have one kind of number which is a floating point type Copyright © 2007 Addison-Wesley. All rights reserved. 15

Fixed Point Types (Decimal)‏ For business applications (money) round-off errors are not acceptable Essential to COBOL .NET languages have a decimal data type Store a fixed number of decimal digits Operations generally have to be defined in software Advantage: accuracy Disadvantages: limited range, wastes memory Copyright © 2007 Addison-Wesley. All rights reserved. 16

C# decimal Type 128-bit representation Range: 1.0x10-28 to 7.9x1028 Precision: representation is exact to 28 or 29 decimal places (depending on size of number)‏ no roundoff error Copyright © 2007 Addison-Wesley. All rights reserved. 17

Other Primitive Data Types: Boolean Range of values: two elements, one for “true” and one for “false” Could be implemented as bits, but often as bytes Character Stored as numeric codings Most commonly used coding: ASCII An alternative, 16-bit coding: Unicode Complex (Fortran, Scheme, Python)‏ Rational (Scheme)‏ Copyright © 2007 Addison-Wesley. All rights reserved. 18

Character Strings Values are sequences of characters Operations: Assignment and copying Comparison (=, >, etc.) Catenation Substring reference Pattern matching Design issues: Is it a primitive type or just a special kind of array? Should the length of strings be static or dynamic? Copyright © 2007 Addison-Wesley. All rights reserved. 19

Character String Implementations C and C++ Not primitive Use char arrays and a library of functions that provide operations SNOBOL4 (a string manipulation language)‏ Primitive Many operations, including elaborate pattern matching Java String class Copyright © 2007 Addison-Wesley. All rights reserved. 20

String Length Options Static: COBOL, Java’s String class Limited Dynamic Length: C and C++ a special character is used to indicate the end of a string’s characters Dynamic (no maximum): SNOBOL4, Perl, JavaScript Ada supports all three string length options Aid to writability As a primitive type with static length, they are inexpensive to provide--why not have them? Dynamic length is nice, but is it worth the expense? Copyright © 2007 Addison-Wesley. All rights reserved. 21

String Implementation Static length: compile-time descriptor Limited dynamic length: may need run-time descriptor not in C and C++ Dynamic length: needs run-time descriptor; allocation/deallocation is main implementation issue Copyright © 2007 Addison-Wesley. All rights reserved. 22

User-Defined Ordinal Types ordinal type : range of possible values corresponds to set of positive integers Primitive ordinal types integer char boolean User-defined ordinal types enumeration types subrange types Copyright © 2007 Addison-Wesley. All rights reserved. 23

Enumeration Types All possible values, which are named constants, are provided in the definition C example enum days {mon, tue, wed, thu, fri, sat, sun}; Design issues duplication of names coercion rules Aid to readability, e.g., no need to code a color as a number Aid to reliability, e.g., compiler can check: operations (don’t allow colors to be added) No enumeration variable can be assigned a value outside its defined range Ada, C#, and Java 5.0 provide better support for enumeration than C++ because enumeration type variables in these languages are not coerced into integer types Copyright © 2007 Addison-Wesley. All rights reserved. 24

enums in C (and C++)‏ To define an enumerated type in C enum weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; enum weekday today = Tuesday; Use typedef to give the type a name typedef enum weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} weekday; weekday today = Tuesday; By default, values are consecutive starting from 0. You can explicitly assign values Enum months {January=1, February, …}; Copyright © 2007 Addison-Wesley. All rights reserved. 25

Enumerations in Java 1.5 An enum is a new class which extends java.lang.Enum and implements Comparable Get type safety and compile-time checking Implicitly public, static and final Can use either == or equals to compare toString and valueOf are overridden to make input and output easier Copyright © 2007 Addison-Wesley. All rights reserved. 26

Java enum Example Defining an enum type Declaring an enum variable enum Season {WINTER, SPRING, SUMMER, FALL}; Declaring an enum variable Season season = Season.WINTER; toString() and name() give you the string representation of the name // println calls toString(), prints WINTER System.out.println( season); // name()returns WINTER String name = season.name(); Copyright © 2007 Addison-Wesley. All rights reserved. 27

Java enum Example valueOf lets you convert a String to an enum Season s = valueOf(“SPRING”); the ordinal() method returns the position of a particular value in the list of names int n = s.ordinal() // returns 1 the static member values() gives you an iterable list of the enum values for (Season s : Seasons.values()) {…} Copyright © 2007 Addison-Wesley. All rights reserved. 28

Subrange Types A contiguous subsequence of an ordinal type Example: 12..18 is a subrange of integer type Ada’s design type Days is (mon, tue, wed, thu, fri, sat, sun); subtype Weekdays is Days range mon..fri; subtype Index is Integer range 1..100; Day1: Days; Day2: Weekday; Day2 := Day1; Aid to readability Make it clear to the readers that variables of subrange can store only certain range of values Reliability Assigning a value to a subrange variable that is outside the specified range is detected as an error Copyright © 2007 Addison-Wesley. All rights reserved. 29

Implementation of User-Defined Ordinal Types Enumeration types are implemented as integers Subrange types are implemented like the parent types code inserted (by the compiler) to restrict assignments to subrange variables Copyright © 2007 Addison-Wesley. All rights reserved. 30

Pointer and Reference Types A pointer is a variable whose value is an address range of values that consists of memory addresses plus a special value, nil (or null or NULL)‏ Provide the power of indirect addressing Provide a way to manage dynamic memory A pointer can be used to access a location in the area where storage is dynamically created (usually called a heap)‏ Generally represented as a single number What are the scope of and lifetime of a pointer variable? What is the lifetime of a heap-dynamic variable? Are pointers restricted as to the type of value to which they can point? Are pointers used for dynamic storage management, indirect addressing, or both? Should the language support pointer types, reference types, or both? Copyright © 2007 Addison-Wesley. All rights reserved. 31

Pointer Operations Two fundamental operations: assignment and dereferencing Assignment is used to set a pointer variable’s value to some useful address Dereferencing yields the value stored at the location represented by the pointer’s value Dereferencing can be explicit or implicit C++ uses an explicit operation via * j = *ptr sets j to the value located at ptr Copyright © 2007 Addison-Wesley. All rights reserved. 32

Pointer Operations Illustrated Dereferencing a pointer j = *ptr assignment ptr = &j allocation ptr = (int*)malloc( sizeof( int))‏ Copyright © 2007 Addison-Wesley. All rights reserved. 33

Pointer Problems Dangling pointers (dangerous)‏ Garbage A pointer points to a heap-dynamic variable that has been de- allocated Garbage An allocated heap- dynamic variable that is no longer accessible to the user program Copyright © 2007 Addison-Wesley. All rights reserved. 34

Pointers in C and C++ Extremely flexible but must be used with care Pointers can point at any variable regardless of when it was allocated Used for dynamic storage management and addressing Pointer arithmetic is possible Explicit dereferencing and address-of operators void * gives a generic pointer can point to any type cannot be de-referenced but can be assigned to a pointer of a different type Copyright © 2007 Addison-Wesley. All rights reserved. 35

Reference Types C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters Advantages of both pass-by-reference and pass-by- value Java extends C++’s reference variables and allows them to replace pointers entirely References refer to objects (including arrays) only C# includes both the references of Java and the pointers of C++ Copyright © 2007 Addison-Wesley. All rights reserved. 36

Evaluation of Pointers Dangling pointers and dangling objects are problems as is heap management Pointers are like goto's--they widen the range of cells that can be accessed by a variable Pointers or references are necessary for dynamic data structures--so we can't design a language without them Copyright © 2007 Addison-Wesley. All rights reserved. 37