Casting February 2, 2018 1 References Let A be a class and B be a subclass of A. A reference variable of type A may refer to an object of type either A or B. A reference variable of type B may refer to only an object of type B. February 2, 2018 2 References If PoliceDog is a subclass of a class Dog, then the following are both legal: Dog lucy = new Dog(); Dog rusty = new PoliceDog(); The following, however, is not: PoliceDog winston = new Dog(); February 2, 2018 3 1
Casting The notion of converting the type of an object from one type to another is called casting. Widening conversion: a type is converted to a wider type. This is done implicitly. Narrowing conversion: a type is converted to a narrower type. This must be done explicitly. February 2, 2018 4 Widening conversion As before, suppose that PoliceDog is a subclass of a class Dog. PoliceDog rusty = new PoliceDog(); Dog d = rusty; This is an example of widening conversion: converting PoliceDog to its wider class Dog. February 2, 2018 5 Narrowing conversion As before, suppose that PoliceDog is a subclass of a class Dog. Dog d = new PoliceDog(); PoliceDog rusty = (PoliceDog) d; This is an example of narrowing conversion: converting Dog to its wider class PoliceDog. February 2, 2018 6 2
Narrowing conversion As before, suppose that PoliceDog is a subclass of a class Dog. Dog d = new PoliceDog(); PoliceDog rusty = (PoliceDog) d; Narrowing conversion requires some care: a variable must be referring to an object of its target type. February 2, 2018 7 Narrowing conversion As before, suppose that PoliceDog is a subclass of a class Dog. Dog d = new Dog(); PoliceDog lucy = (PoliceDog) d; This is illegal because the variable d does not refer to a PoliceDog object. February 2, 2018 8 Casting with interfaces As before, suppose that the class Dog implements the interface Animal. Dog lucy = new Dog(); Animal a = lucy; This is an example of widening conversion: converting Dog to its interface Animal. February 2, 2018 9 3
Casting with interfaces As before, suppose that the class Dog implements the interface Animal. Animal a = new Dog(); Dog lucy = (Dog) a; This is an example of narrowing conversion: converting Animal to an implemented class Dog. February 2, 2018 10 Primitive data types Recall that, in Java, there are four primitive data types: int integers:..., -1, 0, 1, 2,... double reals:..., 3.14159,... char letters: a, b, c,... boolean boolean values: true, false February 2, 2018 11 Classes for primitive data types For each primitive data type, Java also has a corresponding class: The class Integer for the type int. The class Double for the type double. The class Character for the type char. The class Boolean for the type boolean. February 2, 2018 12 4
Casting A value of a primitive data type can be casted to its corresponding object, and vice versa, both implicitly. int i = 1823; Integer j = i; int k = j; February 2, 2018 13 Casting Note. Integer i = 1823; is equivalent to Integer i = new Integer(1823); February 2, 2018 14 Casting What does this ouput? int i = 1823; Integer j = i; i = 2018; System.out.println( j = + j); Output 1823 February 2, 2018 15 5
Generics February 2, 2018 16 Generics A generic type is a type variable. It is used to denote an unspecified type in a general-purpose class. The actual type is fully specified when it is used at runtime. February 2, 2018 17 Generics This class defines a general-purpose class to represent generic pairs, where A, B are type variables. public class Pair<A, B> { private A first; private B second;... public A getfirst() { return first; February 2, 2018 18 6
Generics The class Pair s type variables A, B are specified when it is used at runtime. Pair<Dog, Cat> p = new Pair<Dog, Cat>(); This instantiates a Dog-Cat pair. Pair<Cat, Cat> q = new Pair<Cat, Cat>(); This instantiates a Cat-Cat pair. February 2, 2018 19 Exceptions February 2, 2018 20 Exceptions Exceptions are unexpected events that occur during program execution. In Java, exceptions are regarded as objects. When an unexpected event occur, an exception as an object is thrown. A thrown exception is then caught by another block to be properly handled. February 2, 2018 21 7
Exceptions Java is equipped with basic exception classes. Exception is the most general exception. RuntimeException is an exception to throw during normal execution. To customize an exception, you can also create your own exception as a subclass of an existing exception class. February 2, 2018 22 Customized exceptions public class MyException extends RuntimeException { public MyException(String message) { super(message); February 2, 2018 23 Throwing exceptions public double average(int[] A) { int sum = 0; for (int i = 0; i < A.length; i++) sum = sum + A[i]; double ave = sum/a.length; return ave; What happens when the length of the array A is 0? February 2, 2018 24 8
Throwing exceptions public double average(int[] A) throws DivisionByZeroException { int sum = 0; for (int i = 0; i < A.length; i++) sum = sum + A[i]; if (A.length == 0) throw new DivisionByZeroException( Error... ); double ave = sum/a.length; return ave; February 2, 2018 25 Catching exceptions When an exception is thrown, the program will terminate unless an exception is caught and properly handled. In Java, exceptions are thrown and caught in try-catch blocks. February 2, 2018 26 try-catch blocks A try block contains code for normal execution with conditional statements that might generate exceptions. A catch block contains code to catch and handle a single exception. Under a single try block, there may be multiple catch blocks. February 2, 2018 27 9
try-catch blocks try { // Code that might generate exceptions... double a = average(a); catch(divisionbyzeroexception e) { // Exception handling code... catch(anotherexception e) { // Exception handling code... February 2, 2018 28 try-catch blocks A throw statement does not have to be physically in a try block. It can be in the dynamic (runtime) scope of a try block. A thrown exception will be caught by the closest catch block (in the dynamic scope) with the matching type. February 2, 2018 29 10