Title: Language Specification
1Language Specification
- Semantics of constructs.
- Definition and use of name-value bindings
- Name Resolution.
- Soundness No conflicting requirements.
- Completeness No ambiguity, no omissions.
2Values, Variables, and Types
3Types
- 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.
4Primitive Types and Values
- Numeric Types
- Integral Types (signed twos 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)
5Numeric 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.
6Boolean 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).
7Reference 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.
8Variable
- 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.
9l-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.
10Variables 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.
11Subtle 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
12Variable 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)
13Type 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 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- 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 -
16Type 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()
17Kinds 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 (194)
- char Þ short, char Þ byte,
- byte Þ char, short Þ char
- may lose sign and magnitude information
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
19Motivation for Primitive Array Type Constraints
- int i 10
- long l i
- byte b (byte) i
- int iA new int5
- byte bA (byte) iA // error
- b bA2 // reason
- long lA (long) iA //error
- lA2 l // reason
20Motivation for Reference Array Type Constraints
- Point i new Point()
- Object l i
- ColoredPoint b (ColoredPoint) i
- // throws exception
- Point iA new Point5
- Object lA (Object) iA
- lA2 l // no
exception - b lA2 // error
- l lA2
- ColoredPoint bA (ColoredPoint) iA
- // throws exception
21Another Example Primitive Array Type
- void f(int a)
- int i a4
- a3 i
-
- long la new long8
- short sa new short8
- f(la) // banned compile-time error
- f(sa) // banned compile-time error
-
22Another Example Reference Array Type
- void f(Point pa)
- pa0 new Point()
-
- Object oa new Object8
- f(oa) // compile-time error
- f((Point) oa) // ClassCastException
- ColoredPoint cpa
- new ColoredPoint8
- f(cpa) // array store exception in f
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.
24class S Þ interface K
class S interface K class C extends S
implements K
C c new C() S s c K k (C) s s
(C) k
25Narrowing 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.
26Type 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
27Conversion 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 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- 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.
30Subsets, 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.
31Examples
- (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
32Further Updates
- Java 1.1 Nested and Inner classes
- Java 5 Generics Enumerated Types
33Parameterized Types Examples
- VectorltStringgt
- SeqltSeqltAgtgt
- CollectionltIntegergt
- PairltString,Stringgt
- // Vectorltintgt
- illegal, primitive types cannot be arguments
- // PairltStringgt
- illegal, not enough arguments
34Type with Wildcard
- void printCollection(Collectionlt?gt 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 ltObjectgt)
35Bounded Wildcards
- interface CollectionltEgt
- boolean addAll(Collectionlt? extends Egt c)
-
-
- (cf. Collection ltEgt)