CSE240 Lecture 05

CSE240 Lecture 05

Introduction to Programming Languages
Data, Data types and Type Checking
(201701)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

January 05, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 05: Data,

    Data types and Type Checking Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 jgs

    Data | Memory management
  3. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 jgs

    Data Types § A set of primary values allowed and operations on these values. § Data types define size and format – used to declare variables. // Java int (4) ( to  ) ; -2,147,483,648 to 2,147,483,647 float (4) 3.4e−038 to 3.4e+038
  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 jgs

    Data Types § A set of primary values allowed and operations on these values. § Data types define size and format – used to declare variables. 00000000000000000000000000000011 int i = 3; 01000000010000000000000000000000 float j = 3.0f; sign exponent fraction
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs

    Type Checking (semantic) § Ensuring that the types of operands of an operator are legal or equivalent to the legal type. "Hello" + 5; 5 > 7 + “Hello” char x = 60 + 5; float a = 2.0; double b = 2.0; int b = 'a';
  6. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs

    Data Types Equivalence (Complex Types) § Structural equivalence Two types are equivalent if they have the same set of values and operations. § Name equivalence Two types are equivalent if they have the same name
  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs

    Data Types Equivalence (Complex Types) class Foo { int data[100]; int count; } class Bar { int data[100]; int count; } Foo x, y; Bar r, s; // name // equivalence // correct x = y; r = s; // incorrect x = r; // structural // equivalence // correct x = y; r = s; // correct x = r;
  8. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 8 jgs

    Type Conversion § Coercion –implicitly convert Type_A to Type_B. § Casting: –explicitly convert Type_A to Type_B. § Type error.
  9. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 jgs

    Type Conversion int x = 5; // Coercion: Implicit type conversion (int to float) float f = 3.14f + x; // Casting: Explicit type conversion (float to int) int i = (int) f + x;
  10. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs

    Strong Type Checking A strongly typed language is one in which: § each name in a program has a single type associated with it; § the type is known at compilation time; § type errors are always reported. Strong Weak C C++ Java
  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs

    Strong Type Checking A strongly typed language is one in which: § each name in a program has a single type associated with it; § the type is known at compilation time; § type errors are always reported. Is strong type checking good? ☐ Yes ☐ No ☐ Maybe
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs

    Strong Type Checking A strongly typed language is one in which: § each name in a program has a single type associated with it; § the type is known at compilation time; § type errors are always reported. Is strong type checking good? Trade flexibility for reliability! Strong type increase reliability, but it loses the flexibility. float x = 3.0f; // Java float x = 3.0; // C (defvar x 3.0) // LISP
  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs

    Homework Homework 1: Programming with Java Next lecture: Programming with C Read Chapter 2 Topics: data types, input/output, pointers, struct type
  14. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu

    Spring 2018 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.