lec17

lec17 - CSE 8A: Lecture 17 More on Java types and type...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 24 CSE 8A, UCSD LEC 17 CSE 8A: Lecture 17 More on Java types and type checking Multithreaded programming
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 of 24 CSE 8A, UCSD LEC 17 Final exam Final exam will be Tue Mar 18 3:00-5:30 Place: lecture room Closed-book, closed-notes, no calculators. Bring picture ID! Coverage: Everything! A practice final is available online (PDF format) Webboard final exam discussion topic Lecture notes are available online Final review: last lecture
Background image of page 2
Page 3 of 24 CSE 8A, UCSD LEC 17 The importance of types and type-checking Recall that a variable is just a collection of bits (1’s and 0’s) in the computer’s memory How that pattern of bits is interpreted as a meaningful value and what operations are permitted on it depends on the data type of the variable So it is important that the operations performed on a variable are appropriate to its type! Otherwise you have ‘garbage in, garbage out’. Java does type checking to try to ensure that inappropriate things don’t happen Java does two kinds of type checking: Compile-time (also called “static”) type checking Run-time (also called “dynamic”) type checking
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 4 of 24 CSE 8A, UCSD LEC 17 Compile-time type checking Java is a “strongly typed” language This means: The compiler can determine the datatype of every expression in your program at compile time If your program code tries to do something with an expression that is inappropriate to the type of the expression, it is a compile-time error This is good: The compiler is trying to prevent ‘garbage in, garbage out’ bugs in your program But to prevent these compile-time type errors, or to fix ones the compiler has found in your code, you have to clearly understand Java’s type checking rules
Background image of page 4
Page 5 of 24 CSE 8A, UCSD LEC 17 Determining the type of an expression at compile time In a strongly typed language, the compiler can determine the type of every expression. How does it do that, in Java? It depends on what kind of expression it is: to tell the type of a. .. the compiler looks at. .. identifier the type specified in the identifier’s declaration operator application the type rules for the operator, given the type(s) of the operator’s argument(s) method call the return type specified in the method header cast expression the typename in the cast result of “new” the name of the constructor that is called literal constant the syntax of the literal constant indexed array expression the type specified in the array declaration
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 6 of 24 CSE 8A, UCSD LEC 17 Type rules enforced at compile time The type of every expression can be determined by the compiler, but some types are not allowed in some contexts, to try to prevent potentially meaningless operations. .. These are the rules the compiler will enforce: Arguments to numerical, comparison, and boolean operators: See Lecture 3 Arguments to the assignment operator (see Lecture 3 and 8): You can assign an expression of a certain type to a variable of the same type. You can assign an expression of a numerical type to a variable of any “wider” type.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/12/2008 for the course CSE 8 taught by Professor Marx during the Fall '08 term at UCSD.

Page1 / 24

lec17 - CSE 8A: Lecture 17 More on Java types and type...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online