Language Specification Semantics of constructs Definition and use













![Casting class Prim. Cast { public static void main(String[] argv) { byte b = Casting class Prim. Cast { public static void main(String[] argv) { byte b =](https://slidetodoc.com/presentation_image_h2/7343307b48905d185cf0325fb845d9ca/image-14.jpg)






![Another Example: Primitive Array Type void f(int[] a) { int i = a[4]; a[3] Another Example: Primitive Array Type void f(int[] a) { int i = a[4]; a[3]](https://slidetodoc.com/presentation_image_h2/7343307b48905d185cf0325fb845d9ca/image-21.jpg)
![Another Example: Reference Array Type void f(Point[] pa) { pa[0] = new Point(); } Another Example: Reference Array Type void f(Point[] pa) { pa[0] = new Point(); }](https://slidetodoc.com/presentation_image_h2/7343307b48905d185cf0325fb845d9ca/image-22.jpg)



![Type Errors : Arrays class Type. Errors { public static void main(String [] args) Type Errors : Arrays class Type. Errors { public static void main(String [] args)](https://slidetodoc.com/presentation_image_h2/7343307b48905d185cf0325fb845d9ca/image-26.jpg)









- Slides: 35

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

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

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

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

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 X 86 : LSB-MSB • Calculations involving byte/short done by promoting them to int. • Internally, Java may even store byte/short as int, except in arrays. CS 884 (Prasad) Java Types 5

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

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

Variable A variable is a storage location. • It has an associated type called its compiletime 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. CS 884 (Prasad) Java Types 8

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 rhs x denotes its r-value. CS 884 (Prasad) Java Types 9

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

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

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

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. CS 884 (Prasad) Java Types 13
![Casting class Prim Cast public static void mainString argv byte b Casting class Prim. Cast { public static void main(String[] argv) { byte b =](https://slidetodoc.com/presentation_image_h2/7343307b48905d185cf0325fb845d9ca/image-14.jpg)
Casting class Prim. Cast { 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 ); }} CS 884 (Prasad) Java Types 14

class Point { } class Colored. Point extends Point { } class XYZ extends Point { } class Ref. Cast { public static void main (String [] args) { Object o. R; Point p. R = null; Colored. Point cp. R = null; o. R = p. R; p. R = cp. R; cp. R = p. R; // *error* cp. R = (Colored. Point) p. R; XYZ x = null; cp. R = (Colored. Point) x; // *error* }} CS 884 (Prasad) Java Types 15

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. *; Buffered. Reader bin = new Buffered. Reader (new Input. Stream. Reader (System. in)); • interface variable - class instance Runnable p = new Thread(); CS 884 (Prasad) Java Types 16

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

• Widening Reference Conversions – – – null 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 CS 884 (Prasad) Java Types 18

Motivation for Primitive Array Type Constraints int i = 10; long l = i; byte b = (byte) i; int[] i. A = new int[5]; byte[] b. A = (byte[]) i. A; // error b = b. A[2]; // reason long[] l. A = (long[]) i. A; //error l. A[2] = l; // reason CS 884 (Prasad) Java Types 19

Motivation for Reference Array Type Constraints Point i = new Point(); Object l = i; Colored. Point b = (Colored. Point) i; // throws exception Point[] i. A = new Point[5]; Object[] l. A = (Object[]) i. A; l. A[2] = l; // no exception b = l. A[2] ; // *error* l = l. A[2] Colored. Point[] b. A = (Colored. Point[]) i. A; // throws exception CS 884 (Prasad) Java Types 20
![Another Example Primitive Array Type void fint a int i a4 a3 Another Example: Primitive Array Type void f(int[] a) { int i = a[4]; a[3]](https://slidetodoc.com/presentation_image_h2/7343307b48905d185cf0325fb845d9ca/image-21.jpg)
Another Example: Primitive Array Type void f(int[] a) { int i = a[4]; a[3] = i; } long[] short[] f(la); f(sa); CS 884 (Prasad) la = new long[8]; sa = new short[8]; // banned : compile-time error Java Types 21
![Another Example Reference Array Type void fPoint pa pa0 new Point Another Example: Reference Array Type void f(Point[] pa) { pa[0] = new Point(); }](https://slidetodoc.com/presentation_image_h2/7343307b48905d185cf0325fb845d9ca/image-22.jpg)
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); // Class. Cast. Exception Colored. Point[] cpa = new Colored. Point[8]; f(cpa); CS 884 (Prasad) // array store exception in f Java Types 22

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

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

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. CS 884 (Prasad) Java Types 25
![Type Errors Arrays class Type Errors public static void mainString args Type Errors : Arrays class Type. Errors { public static void main(String [] args)](https://slidetodoc.com/presentation_image_h2/7343307b48905d185cf0325fb845d9ca/image-26.jpg)
Type Errors : Arrays class Type. Errors { public static void main(String [] args) { long [] al = (long []) new int [10]; // compile-time error even with the cast Colored. Point cp = new Point(); // compile-time type error : Need Explicit Cast Colored. Point cp = (Colored. Point ) new Point(); Point [] ap = new Colored. Point [5]; ap [2] = new Point(); // run-time type error : Array. Store. Exception ap [2] = (Colored. Point) new Point(); // run-time type error : Class. Cast. Exception }} CS 884 (Prasad) Java Types 26

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

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

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

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

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

Further Updates • Java 1. 1: Nested and Inner classes • Java 5: Generics; Enumerated Types CS 884 (Prasad) Java Types 32

Parameterized Types Examples • Vector<String> • // Vector<int> – illegal, primitive types cannot be arguments • Seq<A>> • Collection<Integer> • // Pair<String> – illegal, not enough arguments • Pair<String, String> CS 884 (Prasad) Java Types 33

Type with Wildcard void print. Collection(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>) CS 884 (Prasad) Java Types 34

Bounded Wildcards interface Collection<E> { boolean add. All(Collection<? extends E> c) {} … } • (cf. Collection <E>) CS 884 (Prasad) Java Types 35