1.8k

# CSE240 Lecture 05

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

January 05, 2017

## Transcript

1. ### 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
2. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2022 | 2 jgs

Data | Memory management
3. ### 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
4. ### 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
5. ### 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';
6. ### 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
7. ### 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;
8. ### 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.
9. ### 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;
10. ### 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
11. ### 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
12. ### 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

Questions

15. ### 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.