Slide 1

Slide 1 text

jgs CSE 240 Introduction to Programming Languages Lecture 05: Data, Data types and Type Checking Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 2 jgs Data | Memory management

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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) (-231 to 231 ) ; -2,147,483,648 to 2,147,483,647 float (4) 3.4e−038 to 3.4e+038

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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';

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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;

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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;

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 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

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 13 jgs Questions

Slide 14

Slide 14 text

jgs Coming next Programming with C and the Structural Paradigm

Slide 15

Slide 15 text

jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2022 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.