20145331 "Java Program Design" The 5th Week study summary textbook Learning content Summary
Eighth Chapter
Overview
Java exception mechanism mainly relies on the try, catch, finally, throw, throws five keywords.
The syntax format is as follows:
try{被检代码;}catch(异常类 变量){处理异常的代码(处理方式);}
A pair of curly braces after the keyword try will wrap a piece of code that may have an exception, called the monitoring area. An exception object is created when the Java method has an exception during the run. The exception is thrown outside the monitoring area by the Java Runtime system trying to find a matching catch clause to catch the exception. If there is a matching catch clause, its exception-handling code is run, and the Try-catch statement ends.
The principle of matching is that if the thrown exception object belongs to the exception class of the Catch clause, or to the subclass of the exception class, the generated exception object is considered to match the type of exception caught by the catch block.
Throw or Catch:
Throw Exception *: When a method throws an exception with an error, the method creates the exception object and delivers the runtime system, which contains exception information such as the type of the exception and the state of the program when the exception occurred. The runtime system is responsible for finding and executing the code that handles the exception.
catch Exception : After the method throws an exception, the runtime system will look for the appropriate exception handler (exception handler). A potential exception handler is a collection of methods that persist in the call stack, in turn, when an exception occurs. An appropriate exception handler is the exception type that can be handled by the exceptions handler when it matches the type of exception thrown by the method. The runtime system starts with the method in which the exception occurred and then turns back to the method in the call stack until it finds the method that contains the appropriate exception handler and executes it. The runtime system terminates when the runtime system traverses the call stack without finding an appropriate exception handler. At the same time, it means the termination of the Java program.
Finally
Finally, create a block of code. The code block executes before another try/catch occurs after one try/catch block is complete. The finally block executes regardless of whether an exception is thrown. If the exception is thrown, finally even the catch clause that does not match the exception is executed. A method will return from a try/catch block to the calling program at any time, after an uncaught exception or an unambiguous return statement, and the finally clause will still execute before the method returns. This is useful in closing the file handle and releasing any other resources that are allocated at the beginning of the method. The finally clause is optional and can or may not be. Each try statement, however, requires at least one catch or finally clause.
Nineth Chapter
Collection Frame structure:
The collection framework makes it extremely convenient for programmers to manipulate batches of data or object elements. These interfaces and classes have many APIs that operate on abstract data types, with three cores: List, set, and map.
List:
The list is an ordered collection, using this interface to precisely control where each element is inserted. The user is able to access the elements in the list using an index (where the element is located in the list, similar to an array of >), similar to an array of java.
ArrayList: The same vector is an array-based list, but the difference is that ArrayList is not synchronous. So it's better to be more performance than a vector, but when running into a multithreaded environment, you need to manage your thread's synchronization problems.
LinkedList: LinkedList differs from the previous two list, which is not array-based, so it is not limited by the performance of the array.
Set:
Set is an unordered collection that does not contain duplicate elements.
Map:
A map is a container that associates a key object with a value object, and a value object can be a map, and so on, so that a multilevel map can be formed. For a key object, like set, a key object in a map container does not allow repetition, which is to keep the consistency of the search results, and if there are two key objects, then you have a problem with the value object that the key object has, and you may not get the value object you want, and the result will be confusing. So the uniqueness of the key is very important, and it conforms to the nature of the set. Of course, in the process of use, the value object corresponding to a key may change, then the last modified value object corresponds to the key. There is no unique requirement for a value object, and you can map any number of keys to a value object without any problems (although it may be inconvenient for you to use it, you do not know what you are getting for the value object that corresponds to that key).
Problems in teaching materials learning and the solving process
• Textbook Code Exercises:
The user enters an integer consecutively, and the average value of the input number is displayed after entering 0
public class Average {public static void main(String[] args) { Scanner console = new Scanner(System.in); double sum = 0; int count = 0; while(true) { int number = console.nextInt(); if(number == 0) { break; } sum += number; count++; } System.out.printf("平均 %.2f%n", sum / count);}}
You can use try, catch, can display a friendly error message when the error occurs, using the knowledge of this chapter to do the following:
public class Average2 {public static void main(String[] args) { try { Scanner console = new Scanner(System.in); double sum = 0; int count = 0; while (true) { int number = console.nextInt(); if (number == 0) { break; } sum += number; count++; } System.out.printf("平均 %.2f%n", sum / count); } catch (InputMismatchException ex) { System.out.println("必须输入整数"); }}
You can also try to recover the program's normal execution process after catching processing errors, as follows:
public class Average3{public static void main(String[] args){ Scanner console = new Scanner(System.in); double sum = 0; int count = 0; while (true) { try { int number = console.nextInt(); if (number == 0) { break; } sum += number; count++; } catch (InputMismatchException ex) { System.out.printf("略过非整数输入:%s%n", console.next()); } } System.out.printf("平均 %.2f%n", sum / count);}
Learning progress Bar
|
Lines of code (new/cumulative) |
Blog volume (Add/accumulate) |
Learning time (new/cumulative) |
Important growth |
| Goal |
3500 rows |
20 articles |
300 hours |
|
| First week |
150/150 |
1/1 |
14/14 |
|
| Second week |
150/300 |
1/2 |
14/28 |
|
| Third week |
100/400 |
1/3 |
14/42 |
Learned the managed code, learned to use the construction model method to understand the class and object this part of knowledge. |
| Week Four |
200/600 |
1/4 |
14/56 |
|
| Week Five |
317/917 |
1/5 |
14/70 |
|
Other (sentiment, thinking, etc., optional)
These two chapters feel really difficult, compared to the previous knowledge abstracted a lot, there are a lot of things about the API, by watching the relevant teaching video let me learn a lot of things, and began to try to knock the code, although there are several chapters of the content did not understand, but I knocked to teller the corresponding video, So in this week in the study of these two chapters at the same time I on this, sub-parent class, interface these (may feel simple) master has a better grasp, I do not think there is anything embarrassing, but I really learned something is very satisfied (suddenly can read a lot of books on the previous read code, but there are many do not understand.) In the future, I will be in the completion of the teacher's mandate, but also maintain their own rhythm, really hope to maintain the momentum, summed up this week is on the train was forced to knock code, watching video, and then suddenly stopped.
This week's Code hosting reference
- Java Learning Notes (8th Edition)
- Java Learning Note (8th Edition) Learning Guide
20145331 Java Programming 5th Week of study summary