Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders

Similar presentations


Presentation on theme: "Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders"— Presentation transcript:

1 Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders First quiz in the lab this week, Wednesday section still to write. More info in last Tuesday’s lecture. You are in two groups in onQ – your Lab and your Grader. Assignment 1 due this Friday. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

2 Today StringTokenizer Demo Method Overloading
Winter 2019 CMPE212 4/17/2019 Today StringTokenizer Demo Method Overloading Exceptions and Catching Them Objects in Memory – a Model Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

3 Tokenizing Demo See SystemPropertiesDemo.java
Winter 2019 CMPE212 4/17/2019 Tokenizing Demo See SystemPropertiesDemo.java Includes some old-fashioned string parsing code that uses String class methods only. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

4 Fall 2013 CMPE212 Method Overloading A method can have the same name in many different classes (println(), for example). “Overloading” is when a method name is used more than once in method declarations within the same class. (also like println()…) The rule is that no two methods with the same name within a class can have the same number and/or types of parameters in the method declarations. (The “NOT” rule.) Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

5 Method Overloading - Cont.
Why bother? – Convenience! Java does not have default arguments. Allows the user to call a method without requiring him to supply values for all the parameters. One method name can be used with many different types and combinations of parameters. Allows the programmer to keep an old method definition in the class for “backwards compatibility”. Winter 2019 CMPE212 - Prof. McLeod

6 Method Overloading - Cont.
How does it work? Java looks through all methods until the parameter types match with the list of arguments supplied by the user. If none match, Java tries to cast types in order to get a match. (Only “widening” casting like int to double, however.) Winter 2019 CMPE212 - Prof. McLeod

7 Method Overloading - Cont.
Final notes on overloading: You can have as many overloaded method definitions as you want, as long as they are differentiated by the type and/or number of the parameters listed in the definition. Do not change the return type – that is tacky! See the getInt() and getDouble() overloaded methods in the Exercise 1 IOHelper class for example. Winter 2019 CMPE212 - Prof. McLeod

8 Fall 2013 CMPE212 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 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. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

9 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. Winter 2019 CMPE212 - Prof. McLeod

10 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. Winter 2019 CMPE212 - Prof. McLeod

11 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. Winter 2019 CMPE212 - Prof. McLeod

12 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! Winter 2019 CMPE212 - Prof. McLeod

13 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.) Winter 2019 CMPE212 - Prof. McLeod

14 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 { }] Winter 2019 CMPE212 - Prof. McLeod

15 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. Winter 2019 CMPE212 - Prof. McLeod

16 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 Winter 2019 CMPE212 - Prof. McLeod

17 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 Winter 2019 CMPE212 - Prof. McLeod

18 Try With Resources This new syntax is very useful with Java 7 & newer versions’ improved file I/O syntax. More on this topic in exercise 4. Winter 2019 CMPE212 - Prof. McLeod

19 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 { }] Winter 2019 CMPE212 - Prof. McLeod

20 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. Winter 2019 CMPE212 - Prof. McLeod

21 One-Dimensional Arrays - Declaration
Fall 2013 CMPE212 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. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

22 Aside – Java 10 Array Declaration
This: int[] testArray = new int[10]; Can now be simplified to: var testArray = new int[10]; Reduces redundant declarations in the instantiation statement. Winter 2019 CMPE212 - Prof. McLeod

23 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. Winter 2019 CMPE212 - Prof. McLeod

24 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 Winter 2019 CMPE212 - Prof. McLeod

25 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. Winter 2019 CMPE212 - Prof. McLeod

26 A “Null Pointer” After int[] testArray; int[] testArray null 0180ff …
Winter 2019 CMPE212 - Prof. McLeod

27 After the new Keyword After testArray = new int[10]; int[] testArray
0480ff 0180ff int[] testArray 0480ff 10 Winter 2019 CMPE212 - Prof. McLeod

28 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 Winter 2019 CMPE212 - Prof. McLeod

29 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 Winter 2019 CMPE212 - Prof. McLeod

30 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. Winter 2019 CMPE212 - Prof. McLeod

31 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 Winter 2019 CMPE212 - Prof. McLeod

32 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]; Winter 2019 CMPE212 - Prof. McLeod

33 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 Winter 2019 CMPE212 - Prof. McLeod

34 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 Winter 2019 CMPE212 - Prof. McLeod

35 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 Winter 2019 CMPE212 - Prof. McLeod

36 Aliasing Objects - Array Example, Cont.
// after garbage collection Poof! 1 2 3 4 5 0480ff int[] first 0480ff int[] second 0480ff .length 5 Winter 2019 CMPE212 - Prof. McLeod

37 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. Winter 2019 CMPE212 - Prof. McLeod

38 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 Winter 2019 CMPE212 - Prof. McLeod

39 Aliasing Objects - Array Example, Cont.
Fall 2013 CMPE212 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! Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

40 Aliasing Objects Passing an Object into a method results in the method’s parameter being aliased to the Object passed. Called “Passing by Reference”! Winter 2019 CMPE212 - Prof. McLeod


Download ppt "Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders"

Similar presentations


Ads by Google