Casting. References. References

Similar documents
Interfaces. Interfaces. Interface Animal

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CSC Java Programming, Fall Java Data Types and Control Constructs

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

type conversion polymorphism (intro only) Class class

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Introduction to Programming Using Java (98-388)

2.6 Error, exception and event handling

Points To Remember for SCJP

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Operators and Expressions

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Lecture Set 4: More About Methods and More About Operators

Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Polymorphism. return a.doublevalue() + b.doublevalue();

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

CS18000: Problem Solving And Object-Oriented Programming

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Implements vs. Extends When Defining a Class

Object-Oriented Programming

Values, Variables, Types & Arithmetic Expressions. Agenda

Generalized Code. Fall 2011 (Honors) 2

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Lecture Set 4: More About Methods and More About Operators

COMP1008 Exceptions. Runtime Error

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Declarations and Access Control SCJP tips

AP CS Fall Semester Final

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Full file at Chapter 2 - Inheritance and Exception Handling

Selected Java Topics

Sorting. Sorting. Selection sort

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

CS313D: ADVANCED PROGRAMMING LANGUAGE

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Zheng-Liang Lu Java Programming 45 / 79

Lecture 4. Types, Memory, Exceptions

Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

Java Primer 1: Types, Classes and Operators

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Today. Book-keeping. Exceptions. Subscribe to sipb-iap-java-students. Collections. Play with problem set 1

What can go wrong in a Java program while running?

Java Classes: Math, Integer A C S L E C T U R E 8

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Object Oriented Java

Exceptions - Example. Exceptions - Example

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11

Object Oriented Programming Exception Handling

ECE 122 Engineering Problem Solving with Java

Introduction to Java

9 Working with the Java Class Library

WES-CS GROUP MEETING #9

C a; C b; C e; int c;

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Chapter 12 Exception Handling

Programming II (CS300)

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Java Threads and intrinsic locks

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CS 61B Discussion 5: Inheritance II Fall 2014

Declaration and Memory

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

Chapter 13 Exception Handling

Programming II (CS300)

ITI Introduction to Computing II

Final Exam Practice Questions

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Computer Science II (20082) Week 1: Review and Inheritance

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

What is Inheritance?

Exception-Handling Overview

1z z Java SE 8 Programmer I

Solutions Manual. Data Structures and Algorithms in Java, 5th edition International Student Version. M. T. Goodrich and R.

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

Use the scantron sheet to enter the answer to questions (pages 1-6)

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

First IS-A Relationship: Inheritance

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

ITI Introduction to Computing II

Main Topics. Study Tactics. Exam Structure, Roughly. Java Fundamentals. CISC 3120 Midterm Review Guide

XC Total Max Score Grader

1 Shyam sir JAVA Notes

Arrays in Java Multi-dimensional Arrays

COMP6700/2140 Data and Types

Conversions and Casting

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Transcription:

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