Data types and Type Checking Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
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
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
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';
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
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;
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;
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
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
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
[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.