language specification

35
CS 884 (Prasad) Java Types 1 Language Specification • Semantics of constructs. – Definition and use of name-value bindings: Name Resolution. • Soundness : No conflicting requirements. • Completeness : No ambiguity, no omissions.

Upload: blanca

Post on 07-Jan-2016

47 views

Category:

Documents


0 download

DESCRIPTION

Language Specification. Semantics of constructs. Definition and use of name-value bindings: Name Resolution . Soundness : No conflicting requirements . Completeness : No ambiguity, no omissions. Values, Variables, and Types. Subtyping Relation. Types. Primitive Types Reference Types - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Language Specification

CS 884 (Prasad) Java Types 1

Language Specification• Semantics of constructs.

– Definition and use of name-value bindings:

Name Resolution.

• Soundness : No conflicting requirements.• Completeness : No ambiguity, no omissions.

Page 2: Language Specification

CS 884 (Prasad) Java Types 2

Values, Variables, and Types

Subtyping Relation

Page 3: Language Specification

CS 884 (Prasad) Java Types 3

Types

• Primitive Types

• Reference Types

• No composite types such as structures or unions.

• The null type is a special type with one value: the literal null.

Page 4: Language Specification

CS 884 (Prasad) Java Types 4

Primitive Types and Values• Numeric Types

– Integral Types (signed two’s complement)• byte, short (2 bytes), int (4), long (8)

• char (16-bit Unicode)

– Floating-point Types (IEEE 754 Standard)• float (4 bytes), double (8 bytes)

• Boolean Type• boolean (true, false)

Page 5: Language Specification

CS 884 (Prasad) Java Types 5

Numeric Types• Explicit range and behavior.

• Java trades performance for cross-platform portability.

• byte : appropriate when parsing a network protocol or file format, to resolve “endianess”.

– BIG endian : SPARC, Power PC : MSB-LSB– LITTLE endian : Intel X86 : LSB-MSB

• Calculations involving byte/short done by promoting them to int.

• Internally, Java may even store byte/short as int, except in arrays.

Page 6: Language Specification

CS 884 (Prasad) Java Types 6

Boolean Type• Distinct from int type.

– 0 (1) are not false (true). (Cf. C/C++)

• Let boolean b,c and int i.•if (b) {} else {};

•if (b == true) { c = false; } else { c = true; };

equivalent to c = ! b;

•if (i = 0) {};

is a type error (“int used bool expected”).

Page 7: Language Specification

CS 884 (Prasad) Java Types 7

Reference Types and Values

• Class Types• String is a class.

• Interface Types

• Array Types

An object (resp. array object) is an instance of a class (resp. an array).

A reference is a handle for (pointer to, address of) an object or an array object.

Page 8: Language Specification

CS 884 (Prasad) Java Types 8

Variable

A variable is a storage location.

• It has an associated type called its compile-time type.

• It always contains a value that is assignment compatible with its type.– Compatibility of the value of a variable with its

type is guaranteed by the language.

Page 9: Language Specification

CS 884 (Prasad) Java Types 9

l-value and r-value of a variable

• The l-value is the address of the storage location.

• The r-value is the contents of the storage location.

• For the assignment statement: “x = x” the lhs x denotes its l-value, the rhsrhs x denotes its r-value.

Page 10: Language Specification

CS 884 (Prasad) Java Types 10

Variables and Values

• A variable of a primitive type always holds a value of that exact primitive type.

• A variable of a reference type can hold either a null referencereference or a referencereference to any object whose class is assignment compatible with the type of the variable.

Page 11: Language Specification

CS 884 (Prasad) Java Types 11

Subtle Differences• Assignment (x = y)

• Primitive type : copying a value

• Reference type: sharing an object

• final variable modifier• Primitive type : constant value

• Reference type: constant object– Mutable : state of the object changeable

– Immutable : state of the object constant

• E.g., class String

Page 12: Language Specification

CS 884 (Prasad) Java Types 12

Variable Initialization• Each class/instance variable and each array

component is automatically initialized to a fixed default value depending on its type.

• Each formal parameter and each exception parameter is initialized with the argument value.

• In contrast, a local variable is not initialized automatically. However, the compiler checks to ensure that it is not used before it has been assigned a value. (Definite Assignment)

Page 13: Language Specification

CS 884 (Prasad) Java Types 13

Type Conversions

• Every expression has a type deducible at compile-time.

• A conversion from type S to type T allows an expression of type S to be treated as having type T, at compile-time.

• A conversion can require some action at run-time.

Page 14: Language Specification

CS 884 (Prasad) Java Types 14

Casting class PrimCast {

public static void main(String[] argv) { byte b = 0;

int i = b; // widening float f = i; // widening

b = i; // *error* b = (b * 0); // *error* b = (byte) i; b = (byte) 280; // = 24

b = 128; // *error* char four = (char) ( ‘1’ + 3 );}}

Page 15: Language Specification

CS 884 (Prasad) Java Types 15

class Point { }class ColoredPoint extends Point { }class XYZ extends Point { }class RefCast { public static void main (String [] args) {

Object oR; Point pR = null; ColoredPoint cpR = null; oR = pR; pR = cpR; cpR = pR; // *error* cpR = (ColoredPoint) pR; XYZ x = null; cpR = (ColoredPoint) x; // *error* }}

Page 16: Language Specification

CS 884 (Prasad) Java Types 16

Type Compatibility Examples• class variable - subclass instanceimport java.awt.*; import java.applet.*;

Panel p = new Applet();Applet a = (Applet) p;

p = a;import java.io.*;

BufferedReader bin = new BufferedReader

(new InputStreamReader (System.in));

• interface variable - class instanceRunnable p = new Thread();

Page 17: Language Specification

CS 884 (Prasad) Java Types 17

Kinds of Conversion• Identity Conversions• String Conversions• Widening Primitive Conversions (19)

– byte short int long float double– char int long float double

• int float may lose precision

• Narrowing Primitive Conversions (19+4)– char short, char byte, – byte char, short char

• may lose sign and magnitude information

Page 18: Language Specification

CS 884 (Prasad) Java Types 18

• Widening Reference Conversions– null type any reference type– any reference type class Object – class S class T, if S extends T– class S interface K, if S implements K– interface J interface K, if J extends K– array SC [] array TC [], if SC TC,

and both SC and TC are reference types. – array T interface Cloneable

– array T interface Serializable

Page 19: Language Specification

CS 884 (Prasad) Java Types 19

Motivation for Primitive Array Type Constraints

int i = 10; long l = i; byte b = (byte) i;

int[] iA = new int[5];

byte[] bA = (byte[]) iA; // error

b = bA[2]; // reason

long[] lA = (long[]) iA; //error

lA[2] = l; // reason

Page 20: Language Specification

CS 884 (Prasad) Java Types 20

Motivation for Reference Array Type Constraints

Point i = new Point();

Object l = i; ColoredPoint b = (ColoredPoint) i; // throws exception

Point[] iA = new Point[5];Object[] lA = (Object[]) iA; lA[2] = l; // no exception b = lA[2] ; // *error* l = lA[2]

ColoredPoint[] bA = (ColoredPoint[]) iA;

// throws exception

Page 21: Language Specification

CS 884 (Prasad) Java Types 21

Another Example: Primitive Array Type

void f(int[] a) { int i = a[4]; a[3] = i;}

long[] la = new long[8];short[] sa = new short[8];f(la); // banned : compile-time errorf(sa); // banned : compile-time error

Page 22: Language Specification

CS 884 (Prasad) Java Types 22

Another Example: Reference Array Type

void f(Point[] pa) { pa[0] = new Point();}

Object[] oa = new Object[8]; f(oa); // compile-time error

f((Point[]) oa); // ClassCastException

ColoredPoint[] cpa = new

ColoredPoint[8];

f(cpa); // array store exception in f

Page 23: Language Specification

CS 884 (Prasad) Java Types 23

• Narrowing Reference Conversions– Permitted among reference types that can

potentially share common instances.

– These conversions require run-time test to determine if the actual reference is a legitimate value of the new type.

• Observe the interpretation of final class.

• Observe that every non-final class can potentially be extended to implement an interface.

Page 24: Language Specification

CS 884 (Prasad) Java Types 24

class S;interface K;class C extends+ S implements+ K;

class S interface K

C

SS KK

C c = new C();S s = c;K k = (C) s; s = (C) k;

Page 25: Language Specification

CS 884 (Prasad) Java Types 25

Narrowing Reference Conversions– class Object any reference type

– class S class T, if T extends S

– class S interface K, if S is not final and S does not implement K

– interface K class T, if T is not final

– interface K class T, if T is final and T implements K

– interface J interface K, if K does not extend J and there is no common method with same signature but different return types.

– array SC [] array TC [], if SC TC, and both SC and TC are reference types.

Page 26: Language Specification

CS 884 (Prasad) Java Types 26

Type Errors : Arraysclass TypeErrors {

public static void main(String [] args) {

long [] al = (long []) new int [10];

// compile-time error even with the cast

ColoredPoint cp = new Point();

// compile-time type error : Need Explicit Cast

ColoredPoint cp = (ColoredPoint ) new Point();

Point [] ap = new ColoredPoint [5];

ap [2] = new Point();

// run-time type error : ArrayStoreException

ap [2] = (ColoredPoint) new Point();

// run-time type error : ClassCastException }}

Page 27: Language Specification

CS 884 (Prasad) Java Types 27

Conversion Contexts• String Conversions

– any primitive / reference / null type type String

• Applied to an operand of the binary operator + when one of the argument is a String. (The operator + stands for string concatenation.)

• Numeric Promotion• Applied to operands of an arithmetic operator.

Page 28: Language Specification

CS 884 (Prasad) Java Types 28

• Assignment Conversions

v = e;

• type of expression e is assignment compatible with type of variable v, if type(e) type(v), using:– identity conversion

– widening (primitive / reference) conversion

– narrowing primitive conversion from an int constant to byte, short, or char, without overflow.

Page 29: Language Specification

CS 884 (Prasad) Java Types 29

• Method invocation conversion differs from assignment conversion by banning narrowing conversions for int constants. – This simplifies resolution of calls to overloaded

methods with integral type arguments.

• Casting conversion is applied to the operand of a cast operator. A cast can do any permitted conversion.

Page 30: Language Specification

CS 884 (Prasad) Java Types 30

Subsets, subclasses, subtypes• Subsets

– Natural Numbers Integers Reals

• Subclasses– class S is a subclass of class T, if class S can

potentially reuse the implementation for the methods of class T.

• Subtypes– type S is a subtype of type T, if an object of type S

can always replace an object of type T. In other words, they share a common behavior.

Page 31: Language Specification

CS 884 (Prasad) Java Types 31

Examples

• (Int, +, ) is a subtype of (Double,+,), but is not its subclass. However, (Int, +, ,*,/) is not a subtype of (Double,+,,*,/).

• A list-based Bounded Stack can be a subclass of a list-based Stack, but a Bounded stack is not a subtype of Stack..

• An array-based Bounded Stack can be a subtype of list-based Bounded Stack, and vice versa.– subclass : subtype :: code sharing : behavior sharing

Page 32: Language Specification

CS 884 (Prasad) Java Types 32

Further Updates

• Java 1.1: Nested and Inner classes

• Java 5: Generics; Enumerated Types

Page 33: Language Specification

CS 884 (Prasad) Java Types 33

Parameterized Types Examples

• Vector<String>

• Seq<Seq<A>>

• Collection<Integer>

• Pair<String,String>

• // Vector<int> – illegal, primitive

types cannot be arguments

• // Pair<String> – illegal, not enough

arguments

Page 34: Language Specification

CS 884 (Prasad) Java Types 34

Type with Wildcard

void printCollection(Collection<?> c) { // a wildcard collection for (Object o : c) { System.out.println(o); } }• Use of unbounded wildcard allows any

kind of collection to be used as a parameter.– (cf. Collection <Object>)

Page 35: Language Specification

CS 884 (Prasad) Java Types 35

Bounded Wildcards

interface Collection<E> {

boolean addAll(Collection<? extends E> c) {}

}

• (cf. Collection <E>)