1 / 12

Types in programming languages

Types in programming languages. What are types, and why do we need them?. 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.}.

latham
Download Presentation

Types in programming languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Types in programming languages 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.} 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 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. 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). 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. 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 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. 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 Types in programming languages

  10. 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 Subtypes and substitution (2) 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). 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”) Types in programming languages

More Related