DOC PREVIEW
UT Arlington CSE 3302 - Data Types

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

2008-2-41CSE 3302 Programming LanguagesData TypesChengkai Li, Weimin HeSpring 2008Data TypesLecture 7 – Data Types, Spring 20081CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Data Types• What is a data type?A name with certain attributes:– The values that can be stored, the internal representation, the operations, …• A data type is a set of values• e.g., int in Java:int x;x∈Integers= [-2147483648, 2147483647]• A data type is also a set of operations on the valuesLecture 7 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20082Why are data types important?• Example: z = x / y; (Java)– int x, y; x=5; y=2;• Integer division, x/y results in 2.•int z: z=2;int z: z 2;• double z: z=2.0;– double x, y; x=5; y=2;• floating-point division, x/y results in 2.5• int z: wrong!• double z: z=2.5;Lecture 7 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20083Java Types Primitive Reference Lecture 7 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20084Type structure of JavaNumericclass interface double boolean short Integral float Floating point int char Array long byte C Types Numeric Pointer Function struct union void Basic Derived Array Lecture 7 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20085double long double short int Integral float Floating enum int char (signed) (unsigned) long int Type structure of CSimple Data Types• No internal structure:e.g., integer, double, character, and boolean.• Often directly supported in hardware.– machine dependencyMdfid il•Most predefined types are simple types.– Exceptions: String in Java.• Some simple types are not predefined• Enumerated types• Subrange typesLecture 7 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200862008-2-42Enumerated TypesOrdered set, whose elements are named and listed explicitly.• Examples:enum Color_Type {Red, Green, Blue}; ( C )tClTi(RdG Bl)(Ada)type Color_Type is (Red, Green, Blue); ( Ada )datatype Color_Type = Red | Green | Blue; ( ML )• Operations: ?Successor and predecessorLecture 7 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20087Ada Exampletype Color_Type is (Red, Green, Blue);x : Color_Type := Green;x : Color_Type’Succ(x);x : Color_Type’Pred(x);put(x); -- prints GREEN• No assumptions about the internal representation of values• Print the value name itselfLecture 7 – Data Types, Fall 2007CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20078Pascal Exampletypecardsuit = (club, diamond, heart, spade);card = recordsuit: cardsuit; value: 1 .. 13; end; varhand: array [ 1 .. 13 ] of card; • Succ(diamond) = heart; Pred(spade) = heart;• club < heart; is true.• for acard := club to heart doLecture 7 – Data Types, Fall 2007CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20079C Example#include <stdio.h>enum Color {Red, Green, Blue};enum Courses {CSE1111=1, CSE3302=3, CSE3310=3, CSE5555=4};main() {enum Color x = Green;enum Courses c = CSE3302;x++x++;printf("%d\n“,x);printf("%d\n",Blue+1);printf("%d\n",c);return 0;}• Enum in C is simply int• Can customize the valuesLecture 7 – Data Types, Fall 2007CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200710Java Examplepublic enum Planet { MERCURY (2.4397e6), EARTH (6.37814e6);private final double radius; // in meters Planet(double radius) {this.radius = radius; } private double radius() { return radius; } public static void main(String[] args) {p(g[]g){for (Planet p : Planet.values())System.out.printf(“The radius of %s is %f\n", p, p.radius()); } } java.util.Enumeration has different meaningfor (Enumeration e = v.elements() ; e.hasMoreElements() ;) System.out.println(e.nextElement());Lecture 7 – Data Types, Fall 2007CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200711Evaluation of Enumeration Types• Efficiency – e.g., compiler can select and use a compact efficient representation (e.g., small integers)• Readability -- e.g. no need to code a color as a number• Maintainability – e.g., adding a new color doesn’t require updating hard-coded constants.• Reliability -- e.g. compiler can check operations and ranges of value.Lecture 7 – Data Types, Fall 2007CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200712Courtesy of Charles Nicholas at UMBC2008-2-43C Example for Maintainabilityenum Color {White, Green, Blue, Black};enum Color {White, Yellow, Green, Blue, Black};main(){enum Color x = Black;int i = x;while (i >= White){if (i < Green)if (i < Green)printf(“this is a light color!\n”);i--;}}What if no enumeration?if (i < 1) printf(“this is a light color!\n”);Has to be changed to:if (i < 2) printf(“this is a light color!\n”);Lecture 7 – Data Types, Fall 2007CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200713Ada Example for Reliabilitytype DAY is (MON, TUE, WED, THU, FRI, SAT, SUN); type DIRECTION is (NORTH, EAST, SOUTH, WEST);GOAL : DIRECTION;TODAY : DAY;START : DAY; TODAY := MON;GOAL := WEST;START := TODAY; TODAY := WEST; -- Illegal: WEST is not a DAY valueTODAY := 5; -- Illegal: 5 is not a DAY valueTODAY := TODAY + START; -- Illegal: "+" is not defined for DAYSLecture 7 – Data Types, Fall 2007CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200714Subrange TypesContiguous subsets of simple types, with a least and greatest element. • Example: type Digit_Type is range 0..9; ( Ada)• Not available in C,C++,Java. Need to use something like:byte digit; //-128..127...if (digit>9 || digit <0) throw new DigitException();• defined over ordinal types:– ordered, every value has a next/previous element• E.g., integer, enumerations, and subrange itselfLecture 7 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200815Type constructors: Defining New Types• Type constructors as set operations:– Cartesian product– Union–SubsetLecture 7 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200816Subset– Functions (Arrays)• Some type constructors do not correspond to set operations(e.g., pointers)• Some set operators don’t have


View Full Document

UT Arlington CSE 3302 - Data Types

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download Data Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?