Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.

Similar presentations


Presentation on theme: "Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability."— Presentation transcript:

1 Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability times will change to match each lab as the week progresses. Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Today Exceptions Using Arrays to Model Pointers Aliasing Objects
Fall 2017 CISC124 - Prof. McLeod

3 Fall 2013 CISC124 Exceptions How can a method indicate that it is unable to return what it is supposed to return? How can a method deliver details about the error condition? How else can you prevent the instantiation of an Object? The limitation of only returning a single “thing” means that you either designate error values for the “thing” or you have some other way to return the indication of an error. Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

4 Exceptions - Cont. The designers of Java followed conventions used by many other OOP languages - they allowed for another way to get something out of a method. However, an exception is thrown, not returned. Exceptions are Objects (big surprise!). When an error condition is encountered, a method can throw an instance of a pre-defined exception Object. A method can throw several exceptions, one for each possible kind of error condition. Fall 2017 CISC124 - Prof. McLeod

5 Exceptions - Cont., Propagation
If a method throws an exception, then that method is immediately halted and there is no need for any return value, even if the method is non-void. The invoking method then receives the exception – if it does not catch it, then it goes to the next invoking method – all the way to main, if necessary. Called “cascading”. Finally, if main does not catch the exception, your program crashes and a message is sent to the console window. Fall 2017 CISC124 - Prof. McLeod

6 Exceptions - Cont., Message Handler
How does an Exception Object contain information about the error condition? The type of the Object: IOException NumberFormatException FileNotFoundException ArrayIndexOutOfBoundsException So, the method should throw a relevant exception object. Turns out that exceptions can also carry a String message. Fall 2017 CISC124 - Prof. McLeod

7 Bearer of "Bad Tidings" Exceptions – Cont. An exception is just a:
The exception itself cannot do anything about the problem. But it can act to stop your program! Fall 2017 CISC124 - Prof. McLeod

8 Exceptions - Cont., Catching
This is done with a “try/catch” block (see the next slide for the syntax). The compiler will force you to use try/catch blocks when you invoke methods that throw exceptions. (Eclipse can help build a try/catch: Select the code to be surrounded. Right click and choose “Surround with”, then “Try/catch block. The wizard will automatically choose all the possible exceptions.) Fall 2017 CISC124 - Prof. McLeod

9 Exceptions – Cont. Syntax of a “try-catch block”: try {
// block of statements that might // generate an exception } catch (exception_type identifer) { // block of statements }[ catch (exception_type identifer) { }][ finally { }] Fall 2017 CISC124 - Prof. McLeod

10 Exceptions – Cont. You must have at least one “catch block” after the “try block” (otherwise the try block would be useless!) You can have many catch blocks, one for each exception you are trying to catch. The code in the “finally” block is always executed, whether an exception is thrown, caught, or not. Fall 2017 CISC124 - Prof. McLeod

11 Checked vs Unchecked Exceptions
Checked exceptions must either be caught in a method or thrown from that method (using the throws clause in the method header). Examples: IOException, FileNotFoundException, ClassNotFoundException You should not catch an un-checked exception or an Error. Examples: OutOfMemoryError, StackOverflowError, VirtualMachineError Fall 2017 CISC124 - Prof. McLeod

12 Checked vs Unchecked Exceptions, Cont.
The compiler will ensure that checked exceptions are handled properly. Unchecked exceptions occur only at runtime and the compiler does not care about them. Unchecked exceptions are all sub-classes of java.lang.Error Fall 2017 CISC124 - Prof. McLeod

13 Try With Resources This new syntax is very useful with Java 7 & 8’s improved file I/O syntax. More on this topic in exercise 4. Fall 2017 CISC124 - Prof. McLeod

14 Try With Resources – Cont.
Syntax of a “try-with-resources block”: try (instantiation; instantiation; …) { // other statements that might // generate an exception }[ catch (exception_type identifer) { // block of statements }][ catch (exception_type identifer) { }][ finally { }] Fall 2017 CISC124 - Prof. McLeod

15 Try With Resources – Cont.
The instantiation(s) inside the set of ( ) immediately after the try keyword are declared resources that must be local to the try/catch block. Note that there is no ; at the end of the list and that there does not have to be any catch blocks. These resources must all implement the AutoCloseable interface, which means that the try block can close the resource when it is finished. Resources will be closed whether or not an exception is thrown because their scope is forced to be in the try block only. As a result, all use of the resource must also take place in the try block. Fall 2017 CISC124 - Prof. McLeod

16 One-Dimensional Arrays - Declaration
Fall 2013 CISC124 One-Dimensional Arrays - Declaration As we have already seen - To create an array to hold 10 integers: int[] testArray = new int[10]; testArray now points to an area of memory that holds locations for 10 integers. It also points to one location that holds testArray.length which is an attribute of the array, that is equal to the number of elements. Arrays are Objects in Java. Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

17 Modelling Pointers Use the array as an example to help model the behaviour of a pointer. Note that an array is an example of a mutable object – object contents can be changed by accessing the object’s contents through the pointer. Object design or class design determines mutability. Fall 2017 CISC124 - Prof. McLeod

18 One-Dimensional Arrays - Declaration, Cont.
0480ff 0180ff int[] testArray 0480ff As a “pointer”, testArray points to an area of memory that contains a series of int values as well as the attribute length. 10 .length Fall 2017 CISC124 - Prof. McLeod

19 One-Dimensional Arrays - Declaration, Cont.
The java statement above can be split into two: int[] testArray; testArray = new int[10]; The first statement creates a variable of type int[] - that is to say that it will be an array of int’s. The variable, testArray is now an object of type int[] that contains an “object reference” or a pointer. The object reference is null after the first statement. Fall 2017 CISC124 - Prof. McLeod

20 A “Null Pointer” After int[] testArray; int[] testArray null 0180ff …
Fall 2017 CISC124 - Prof. McLeod

21 After the new Keyword After testArray = new int[10]; int[] testArray
0480ff 0180ff int[] testArray 0480ff 10 Fall 2017 CISC124 - Prof. McLeod

22 One-Dimensional Arrays - Cont.
The array indices allow mutability. Memory locations are calculated by offsets from the base address: testArray[0] testArray[1] testArray[2] testArray[3] testArray[4] testArray[5] testArray[6] testArray[7] testArray[8] testArray[9] testArray.length 10 Array indices Fall 2017 CISC124 - Prof. McLeod

23 Multi-Dimensional Arrays
Consider: int[][] exArray = new int[3][5]; 1002fc int[][] exArray 0480ff int[] exArray[0] 1002fc exArray[1] 1010fc exArray[2] 1201ab 1010fc 1201ab Fall 2017 CISC124 - Prof. McLeod

24 Multi-Dimensional Arrays - Cont.
So exArray points to three one dimensional arrays: exArray[0] exArray[1] exArray[2] Each of these arrays has the same length: exArray[2].length // returns 5 Yes, you can refer to these arrays in code, just like this. Fall 2017 CISC124 - Prof. McLeod

25 Multi-Dimensional Arrays - Cont.
int[][] twoDArray = new int[10][20]; The above is equivalent to: int[][] twoDArray; twoDArray = new int[10][]; for (int i = 0; i < 10; i++) twoDArray[i] = new int[20]; As shown above: twoDArray.length // gives 10 twoDArray[0].length // gives 20 Fall 2017 CISC124 - Prof. McLeod

26 Multi-Dimensional Arrays - Cont.
“Ragged Arrays” are not “square”, and are legal in Java: int[][] raggedArray = new int[5][]; raggedArray[0] = new int[5]; raggedArray[1] = new int[3]; raggedArray[2] = new int[2]; raggedArray[3] = new int[8]; raggedArray[4] = new int[12]; Fall 2017 CISC124 - Prof. McLeod

27 Multi-Dimensional Arrays - Cont.
Array initializer for two-D array, for example: int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; System.out.println(twoDArray.length); // 4 System.out.println(twoDArray[0].length); // 3 Fall 2017 CISC124 - Prof. McLeod

28 Aliasing Objects - Array Example
int[] first = {1, 2, 3, 4, 5}; int[] second = {10, 20, 30, 40, 50, 60, 70}; 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0960ff .length 5 .length 7 Fall 2017 CISC124 - Prof. McLeod

29 Aliasing Objects - Array Example, Cont.
second = first; // Aliasing! 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0480ff .length 5 .length 7 Fall 2017 CISC124 - Prof. McLeod

30 Aliasing Objects - Array Example, Cont.
// after garbage collection Poof! 1 2 3 4 5 0480ff int[] first 0480ff int[] second 0480ff .length 5 Fall 2017 CISC124 - Prof. McLeod

31 Aside – “Garbage Collection” in Java
Some computer programming languages require you to indicate when you are done with variables so the memory they are occupying can be released back to the OS. Called “Garbage Collection”. (Fortunately!) Java has an automatic Garbage Collection system: Variables are garbage collected once you move outside their scope. Object contents are garbage collected when there are no pointers pointing to the contents. Fall 2017 CISC124 - Prof. McLeod

32 Aliasing Objects - Array Example, Cont.
first[4] = 500; // second[4] is also 500 1 2 3 4 500 0480ff int[] first 0480ff int[] second 0480ff .length 5 Fall 2017 CISC124 - Prof. McLeod

33 Aliasing Objects - Array Example, Cont.
Fall 2013 CISC124 Aliasing Objects - Array Example, Cont. So, setting one array to equal another as in: array1 = array2; sets array1 to point to the same data memory location that was (and still is) pointed to by array2. Now, changing the value of an element in array2 will change that same element in array1, or visa-versa - this makes sense since both array Objects point to the same set of data values in memory! Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

34 Aliasing Objects Passing an Object into a method results in the method’s parameter being aliased to the Object passed. Called “Passing by Reference”! Fall 2017 CISC124 - Prof. McLeod


Download ppt "Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability."

Similar presentations


Ads by Google