Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 884 (Prasad)Java Types1 Language Specification Semantics of constructs. –Definition and use of name-value bindings: Name Resolution. Soundness : No.

Similar presentations


Presentation on theme: "CS 884 (Prasad)Java Types1 Language Specification Semantics of constructs. –Definition and use of name-value bindings: Name Resolution. Soundness : No."— Presentation transcript:

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

2 CS 884 (Prasad)Java Types2 Values, Variables, and Types Subtyping Relation

3 CS 884 (Prasad)Java Types3 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.

4 CS 884 (Prasad)Java Types4 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)

5 CS 884 (Prasad)Java Types5 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.

6 CS 884 (Prasad)Java Types6 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”).

7 CS 884 (Prasad)Java Types7 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.

8 CS 884 (Prasad)Java Types8 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.

9 CS 884 (Prasad)Java Types9 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. rhsFor the assignment statement: “x = x” the lhs x denotes its l-value, the rhs x denotes its r-value.

10 CS 884 (Prasad)Java Types10 Variables and Values A variable of a primitive type always holds a value of that exact primitive type. reference referenceA 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.

11 CS 884 (Prasad)Java Types11 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

12 CS 884 (Prasad)Java Types12 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)

13 CS 884 (Prasad)Java Types13 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.

14 CS 884 (Prasad)Java Types14 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 ); }}

15 CS 884 (Prasad)Java Types15 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* }}

16 CS 884 (Prasad)Java Types16 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();

17 CS 884 (Prasad)Java Types17 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

18 CS 884 (Prasad)Java Types18 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

19 CS 884 (Prasad)Java Types19 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

20 CS 884 (Prasad)Java Types20 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

21 CS 884 (Prasad)Java Types21 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

22 CS 884 (Prasad)Java Types22 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

23 CS 884 (Prasad)Java Types23 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.

24 CS 884 (Prasad)Java Types24 class S; interface K; class C extends+ S implements+ K; class S  interface K CSK C c = new C(); S s = c; K k = (C) s; s = (C) k;

25 CS 884 (Prasad)Java Types25 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.

26 CS 884 (Prasad)Java Types26 Type Errors : Arrays class 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 }}

27 CS 884 (Prasad)Java Types27 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.

28 CS 884 (Prasad)Java Types28 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.

29 CS 884 (Prasad)Java Types29 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.

30 CS 884 (Prasad)Java Types30 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.

31 CS 884 (Prasad)Java Types31 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

32 CS 884 (Prasad)Java Types32 Further Updates Java 1.1: Nested and Inner classes Java 5: Generics; Enumerated Types

33 CS 884 (Prasad)Java Types33 Parameterized Types Examples Vector Seq > Collection Pair // Vector –illegal, primitive types cannot be arguments // Pair –illegal, not enough arguments

34 CS 884 (Prasad)Java Types34 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 )

35 CS 884 (Prasad)Java Types35 Bounded Wildcards interface Collection { boolean addAll(Collection c) {} … } (cf. Collection )


Download ppt "CS 884 (Prasad)Java Types1 Language Specification Semantics of constructs. –Definition and use of name-value bindings: Name Resolution. Soundness : No."

Similar presentations


Ads by Google