DOC PREVIEW
UT Arlington CSE 3302 - JAVA

This preview shows page 1-2-3-4-26-27-28-54-55-56-57 out of 57 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 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 57 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 57 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 57 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 57 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 57 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 57 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 57 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 57 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 57 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 57 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 3302 Lecture 2 Java 31 Aug 2010 Nate Nystrom University of Texas at Arlington Review Language paradigms Imperative C Pascal Fortran OO C Java Smalltalk JavaScript Functional ML Haskell LISP Scheme Logic Prolog Datalog OO languages Simula67 Kristen Nygaard Ole Johan Dahl objects classes inheritance virtual methods coroutines Smalltalk 1972 80 Alan Kay Dan Ingalls et al Xerox PARC pure OO everything is an object C 1985 Bjarne Stroustrup Bell Labs C with Simula67 classes Java 1995 James Gosling Guy Steele Bill Joy Sun JavaScript 1995 Brendan Eich Netscape Why is Java your favorite language Java 1995 James Gosling et al Sun C like syntax Portable write once run anywhere Type safe GC rich libraries History Java 1 0 1996 Java 1 1 1997 inner classes reflection Java 1 2 1998 collections library Java 1 3 2000 HotSpot VM Java 1 4 2002 assert NIO Java 5 2004 generics autoboxing annotations enums enhanced for varargs Java 6 2006 concurrency libraries Java 7 2011 closures method handles dynamic language support Language definition Syntax defines the structure of a program set of rules defining which symbols are a legally structured program Semantics defines the meaning of a program without semantics programs are just sequences of characters Java syntax Basically C with a few changes same unary and binary operations same field access syntax same control constructs while for etc same class declaration syntax vs no includes defines class members declared within class body in same file Types What s a type A predicate on what values might occur at run time i has type int means i 231 231 b has type boolean means b false true s has type String means s 0 1 00 Types Can a type be an arbitrary predicate e g x an integer equal to gcd a b In principle yes but might be undecidable or at least intractable theorem proving but automatic theorem provers are getting better and better Types should be efficiently decidable Type systems Languages have type systems Type system describes what are the types what is the type of a given expression is the type of a given expression allowed Types Types What types does Java have Types What types does Java have primitives boolean char byte short int long float double Types What types does Java have primitives boolean char byte short int long float double reference types class types interface types array types Types What types does Java have primitives boolean char byte short int long float double reference types class types interface types array types any others Types What types does Java have primitives boolean char byte short int long float double reference types class types interface types array types any others What is the type of null Types What types does Java have primitives boolean char byte short int long float double reference types class types interface types array types any others What is the type of null What about void Types What types does Java have primitives boolean char byte short int long float double reference types class types interface types array types any others What is the type of null What about void What about methods Types What types does Java have primitives boolean char byte short int long float double reference types class types interface types array types any others What is the type of null What about void What about methods Generics Type safety Type errors Improper type inconsistent operations during program execution Type safety absence of run time type errors Operations unsupported by a value will never occur at run time cannot multiply a string and an int cannot call a function that is not defined cannot access a field that does not exist cannot access outside the bounds of an array Strong vs weak typing A language is strongly typed if it ensures no type errors occur at run time i e it is type safe Contrast with weakly typed Strong vs weak typing Is Java strongly typed or weakly typed C Python Other examples C is weakly typed int x 100 printf s x Static vs dynamic typing statically typed languages rule out type errors at compile time requires programmers to write type declarations dynamically typed languages rule out type errors at run time perform dynamic type checks before each operation Many statically typed languages allow some dynamic type checks e g casts instanceof in Java Static vs dynamic typing Is Java statically typed or dynamically typed C Python Other examples Type systems strong weak sta c dynamic Java C Scala Smalltalk JavaScript Scheme C C Perl Forth assembly code Java s type system Strongly typed Cannot use a value at the wrong type Statically typed Type errors detected at compile type Except when the programmer does an explicit runtime check Casts in Java Can cast an object reference to an object type Compiler will perform a run time type test Bird b new Penguin Penguin b ok Vulture b throws ClassCastException Compiler can reject program if cast will always fail Bird new Elephant Primitive casts in Java Casting from one primitive type to another converts the value to the target type int x 65536 byte b byte x assert b 0 Computing types How to determine what type an expression has e T means e has type T 1 int true boolean new C C Typing rules Language specification defines rules for computing types x y int if x and y both have type byte short char or int x y long if x and y both have type byte short char int or long and at least one is long Can define rules for every expression in the language Typing rules e f T if e has type C and C has a field f of type T a 0 T if a has type T Type checking Compiler computes types and then checks them while c S c must have type boolean int x y y must have type int a i a must be an array type i must have type int Design choices Why did the designers of Java make these choices Object references not pointers Reference types and primitive types are different Single class inheritance multiple interface inheritance Object references Can only have references to objects not arbitrary pointers Cannot have a pointer into the middle of an array Cannot have a pointer to a variable on the stack Cannot do pointer arithmetic like in C C int p a p Primitive types Java distinguishes between primitive types and reference types Done for efficiency reasons int is more efficient than java lang Integer int is much much more efficient than Integer More on this next week Inheritance Java has single class inheritance multiple interface inheritance Avoids most of the complications with multiple


View Full Document

UT Arlington CSE 3302 - JAVA

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download JAVA
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 JAVA 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 JAVA 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?