types in programming languages1 what are types, and why do we need them?

12
Types in programming lang uages 1 Types in programming languages What are types, and why do we need them?

Upload: lydia-robinson

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 1

Types in programming languages

What are types, and why do we need them?

Page 2: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 2

Type definitions

• Type: A collection of values– boolean: true, false– int: -4, 3, 14593, and much more

• Data type: Type + operations to manipulate the type– boolean + {&&, ||, !}– int + {+, -, *, etc.}

Page 3: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 3

Definition: Date item

• Data items– Data, belongs to a type– 34, belongs to int– false belongs boolean

• Simple data item– No subparts

• Aggregate data item– Has subparts– Classes aggregate other types

Page 4: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 4

Abstract data type (ADT)

• ADT: Defines a data type in terms of type + operations– Focus on what, not how.– Java: Interface, or public parts of a class

• Data structure– Physical implementation of an ADT– Focus on how.

• Example: Java collections– ADT: List– Data structures: ArrayList, LinkedList, etc.

Page 5: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 5

Why do we need types?

• A data item is just a name/alias/mnemonic of a memory location, i.e. one or more bytes in memory.

• Using types we can have rules saying which operations can legally be applied to variables (i.e. memory locations).– And which operations to disallow– Example: If 2 variables (memory locations) is

declared String it may not make sense to perform the operation ‘-’ (minus).

Page 6: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 6

Strong vs. weak typing

• Strong typing– Variables, expressions, etc. have types.– Types must “match”.– Languages: Java, C#, and many other languages

• Weak typing– No types.– Variables are just aliases for memory locations (i.e. bytes)– Languages: Assembly, BASIC, and many other languages.

• When strong typing was introduced (late 1960’es) programmers used to say– “Strong typing is for programmers with weak minds”– Meaning: A programmer should be able to remember the “types”

of the variables / memory locations himself.

Page 7: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 7

Static vs. dynamic type checking

• Static type checking– Types of variables, expressions, etc. checked at compile-time.– Java: Uses static type checking

• Dynamic checking– Types of variables, expressions, etc. checked at run-time.– Java: When you use type casts, checking is deferred to run-time.

If something is wrong you get a ClassCastException.

• Goal: Check as much as possible at compile-time– An error message from the compiler to the programmer is much

better than an error message from the program to the end-user.– Java: The main reason for introducing generics in Java 5.0

Page 8: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 8

Types in object-oriented programming

• In object-oriented programming the programmer creates his own types, called classes.

• From these classes we make variables, called objects.

• Types are organized in inheritance hierarchies.– Java (and C#): One single class hierarchy rooted in

class Object.– Other language (like C++): More hierarchies, no

single root.

Page 9: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 9

Subtypes and substitution

• Whenever you have an object of a super type you should be able to substitute that object with an object of a subtype.

• Java example– List mylist;

• // mylist can be an object of ArrayList, LinkedList

– mylist.add(“Anders”)• Works no matter is myList is an ArrayList or

LinkedList

Page 10: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 10

Subtypes and substitution (2)

• class S {– B method(A a) { … }

• }

• class T extends S {– Y method(X x) { … }

• }

• Requirements– Parameters

• A must be a subtype of X

– Return types• Y must be a subtype of B.• Called co-variant return types.

• These requirements are not handled well in many programming languages– Java

• Requires that A is the same type as X

– Otherwise Java assumes we want overloading, not overriding.

• Allows Y to be a subtype of B.– New in Java 5.0

Page 11: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 11

Method overriding in Java

• Overridden methods must have same signature in subtype as in super type.– Same parameters in same order– Return same type or more specific (subtype).– Throw same or more specific (subtype) exceptions.

• Exceptions can be considered a kind of “return types”

• Example: MethodOverriding.java• Idea for future Java version:

– Parameters of same of more general type.• Not considered an overriding in present day Java.• Reason: Method overloading (methods with same name, but

different parameter types (or number of parameters).

Page 12: Types in programming languages1 What are types, and why do we need them?

Types in programming languages 12

Primitive types in object-oriented programming languages

• Most programming languages have primitive types like int, double, char, boolean, etc.

• These types are considered primitive because they are not classes.

• Java: Primitive types and wrapper classes, like int ~ Integer, boolean ~ Boolean, etc.

• Java 5.0: Automatic conversion from primitive to wrapper and vice versa (called “auto boxing”)