360 likes | 609 Views
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
E N D
Language Specification • Semantics of constructs. • Definition and use of name-value bindings: Name Resolution. • Soundness : No conflicting requirements. • Completeness : No ambiguity, no omissions. Java Types
Values, Variables, and Types Subtyping Relation Java Types
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. Java Types
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) Java Types
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. Java Types
Boolean Type • Distinct from int type. • 0 (1) are notfalse (true). (Cf. C/C++) • Let boolean b,c and int i. • if (b) {} else {}; • if (b == true) { c = false; } else { c = true; }; equivalent toc = ! b; • if (i = 0) {}; is a type error (“int used bool expected”). Java Types
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. Java Types
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. Java Types
l-value and r-value of a variable • The l-valueis the address of the storage location. • The r-valueis the contentsof the storage location. • For the assignment statement:“x = x”the lhsxdenotes itsl-value, therhsxdenotes itsr-value. Java Types
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 reference or a reference to any object whose class is assignment compatible with the type of the variable. Java Types
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., classString Java Types
Variable Initialization • Each class/instance variable and each array component is automatically initialized to a fixed defaultvalue 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) Java Types
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. Java Types
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 ); }} Java Types
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* }} Java Types
Type Compatibility Examples • class variable - subclass instance import 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 instance Runnable p = new Thread(); Java Types
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 Java Types
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 Java Types
Motivation for PrimitiveArray 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 Java Types
Motivation for ReferenceArray 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 Java Types
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 error f(sa); // banned : compile-time error Java Types
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 Java Types
Narrowing Reference Conversions • Permitted among reference types that can potentiallysharecommon 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. Java Types
S K C classSÞinterfaceK classS; interfaceK; classCextends+S implements+K; C c = newC(); S s = c; K k = (C) s; s = (C) k; Java Types
Narrowing Reference Conversions • class Object Þ any reference type • class S Þ class T, if T extends S • class S Þinterface K, if S is notfinaland S does notimplement K • interface K Þclass T, if T is not final • interface K Þclass T, if T isfinaland T implementsK • interface J Þinterface K, if K does notextend J andthere 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. Java Types
Type Errors : Arrays classTypeErrors{ public static voidmain(String [] args) { long[] al = (long [])newint[10]; // compile-time error even with the cast ColoredPoint cp = newPoint(); // compile-time type error : Need Explicit Cast ColoredPoint cp = (ColoredPoint ) newPoint(); Point [] ap = newColoredPoint[5]; ap [2] = newPoint(); // run-time type error : ArrayStoreException ap [2] = (ColoredPoint)newPoint(); // run-time type error : ClassCastException }} Java Types
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. Java Types
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 conversionfrom anint constant to byte, short, or char, without overflow. Java Types
Method invocation conversion differs from assignment conversion by banning narrowing conversions for intconstants. • 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. Java Types
Subsets, subclasses, subtypes • Subsets • Natural Numbers Í Integers Í Reals • Subclasses • class S is a subclassof 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. Java Types
Examples • (Int, +, -) is a subtypeof (Double,+,-), but is not its subclass. However, (Int, +, -,*,/) is nota subtype of (Double,+,-,*,/). • A list-based Bounded Stack can be a subclassof a list-based Stack, but a Bounded stack is notasubtype 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 Java Types
Further Updates • Java 1.1: Nested and Inner classes • Java 5: Generics; Enumerated Types Java Types
Vector<String> Seq<Seq<A>> Collection<Integer> Pair<String,String> // Vector<int> illegal, primitive types cannot be arguments // Pair<String> illegal, not enough arguments Parameterized Types Examples Java Types
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>) Java Types
Bounded Wildcards interface Collection<E> { boolean addAll(Collection<? extends E> c) {} … } • (cf. Collection <E>) Java Types