First, exception handling
1. syntax and inheritance structure
All errors in the try, Catch:java are wrapped into objects, and you can try to execute the program (try) and catch (catch) to represent the wrong object and do some processing. Using the try, catch syntax, the JVM tries to execute the program code in the try block, and if an error occurs, the executor jumps off the point of the error, and then executes the program code in the catch if it matches the type declared in the catch bracket, whether it conforms to the type of the wrong object being thrown. There are always some unexpected errors in the program, and errors in Java are presented as objects to the various subclass instances of java.lang.Throwable. You can do some processing for this error as long as you can catch the object that is wrapped incorrectly. Here is an example of a book on using try and catch.
In this procedure, the correct number is entered, the average is calculated, and the result is as follows:
However, if you accidentally enter a value that is not an int, the following occurs:
This is another code for input errors, which is part of the judgment and corrects
If you enter an error, the following result will appear:
2. exception Inheritance structure
Errors are wrapped as objects that can be thrown, so the design Error object inherits from the Java.lang.Throwable class, and Throwable defines the method of getting the error message, stack trace, and so on, which has two subclasses: Java.lang.Error and Java.lang.Exception. This paper briefly describes the difference between the error class and the exception class, and in Java for the more serious problem, it is described by the error class, and for the non-serious problem, it is described by the exception class. For error, it is generally not written with targeted code to handle it because it is beyond the scope of the JVM's ability to run.
On the syntax and inheritance architecture, if a method declaration throws an instance of Throwable or subclasses, as long as it is not part of the error or java.lang.RuntimeException or its subclass, it must be handled explicitly using the try, catch syntax. Or in the method with throws declaration of this method will throw an exception, otherwise it will fail to compile, the use of throws also improve the security of the code.
Exception has a special subclass exception called RuntimeException exception, is a run-time exception, it is characterized by the function of the content throws the exception, the function can not declare, compile the same, if the exception is declared on the function, the caller can not be processed, Compiled as passed. Exception or its sub-object, but not belonging to RuntimeException or its subclass object, known as a inspected exception (Checked Exception), a class instance that is derived from RuntimeException is also known as a non-inspected exception ( Unchecked Exception), the purpose of the exception to be examined is that when the API designer implements a method, some conditions can cause an error, and the client that invokes the method has the ability to handle the error, requiring the compiler to remind the client that it must explicitly handle the error, otherwise it cannot be compiled. The API client does not have the option to process.
If the parent exception object is caught before the subclass exception object, the Catch subclass exception object will never be executed and the compiler will check for this error. Starting with JDK7, you can use the multiple capture syntax, but you still need to be aware of the exception inheritance, and the exceptions listed in the catch brackets must not have an inheritance relationship, or a compilation error will occur.
After the catch block after the partial error processing, you can use throw to throw the exception again, here will throw and throws to distinguish between, throws use on the function, followed by the exception class, can be followed by a number, separated by commas, and throw is used in the function, followed by an exception object.
If you want to know the origin of the anomaly, and the stack propagation of the exception under the multiple method invocation, you can take advantage of the stack trace that the exception object collects automatically to get the relevant information, such as calling the Printstacktrace (), Getstacktrace () of the exception object. To be good at stack tracking, if the program code is not pocketed abnormal behavior, improper handling of the exception, or display incorrect information. In the case of throwing a throw exception, the exception's trace stack start point is still the source of the exception, not the place where the exception was thrown. If you want to make the exception stack start to throw out the exception, you can use Fillinstacktrace (), which reloads the exception stack, sets the starting point to throw the exception in place, and returns the Throwable object.
3. exception and resource management
When an exception is thrown in the program due to an error, the original execution process will be interrupted, the program code after the exception is thrown will not be executed, if the program opened the relevant resources, is it closed after the use is complete?
finally Block
Finally, you must execute the action to close the resource, try, catch syntax can also be paired with finally, regardless of whether there is an exception in the try block, if you write finally block, then finally block will be executed. To give an example:
Since the finally block is bound to be executed, in this example scanner was originally null, if FileInputStream creation fails, then scanner is likely to be null, Therefore, in the finally block, you must first check whether the scanner has a reference object, and then call the close () method further, otherwise scanner reference to null and intend to call the close () method, but will throw nullpointerexception. The object that you want to try to close the resource (try-with-resources) is written in parentheses after the try, and if you don't need a catch to handle any exceptions, you don't have to write or write finally. When you try to close a resource-syntactically-applied object, you must manipulate the Java.lang.AutoCloseable interface, which can also close more than two object resources, as long as the middle is separated by semicolons. In the parentheses of the try, the sooner the object resource is written, the sooner it is closed.
Second, Collection with the Map
1. Understanding the Collection Architecture
Javase provides APIs to meet a variety of needs, before using these APIs, it is recommended to understand their inheritance and interface of the operation of the schema, in order to understand when to adopt which class, and how the class to work with each other, rather than degenerate into the realm of dead-back API or transcription paradigm.
The behavior of the collection object, such as the Add () method of the new object, the Remove () method for removing the object, and so on, are all defined in java.util.Collection. Since the object can be collected, but also to be able to get the object, this is the behavior defined by java.lang.Iterable, it defines the iterable () method to return the Java.util.Iterator operand, you can get the collected objects. The collection interface has three sub-interfaces, list, set, and queue, respectively. If you want the index order of each object to be recorded at the time of collection and retrieve the object by index, You can use the Java.util.List interface, if you want to collect objects that do not repeat, have the behavior of the collection, you can use the Java.util.Set interface, if you want to collect objects in a queue, if the objects are collected to the end, when the object is obtained from the front end, you can use the Java.util.Queue interface, as If you want to join, remove, and so on both ends of the queue, you can use Java.util.Deque.
2. map for key values
Just like web search, according to the keyword can find corresponding data, program design also often this kind of demand, according to a key to obtain the corresponding value. The Java.util.Map interface can be used to manipulate the object to establish the corresponding data of the key, and then to obtain the value, as long as the corresponding key, as long as the corresponding key can be quickly obtained. Common map operation classes are HashMap, TreeMap and properties. HashMap is characterized by thread insecurity, fast speed, allowing null keys to be stored, null values, and TreeMap ordering the key, provided that the object acting as the key must operate the comparable interface. Or an object that specifies the operation comparable interface when creating TreeMap, the Properties setproperty () can specify the key value of the string type, GetProperty () can specify the key of the string type, retrieve the value of the string type, Typically called property names and property values.
Problems in teaching materials learning and the solving process
The P295 Mapkeyvalue.java run results in the book are different from the HashMap display in the book.
Problems in code debugging and the resolution process
In the two chapters I found that some things according to the book, there is no way to compile, and there is the code, such as 266 page, he is used before and after the variable name is not the same, the definition of the object o, behind the pull over a elem, I think the book has errors is normal, Just by the way we can see if we really know about the previous contents of the book and whether we understand the code.
After the objects are collected, sorting the objects is a common action, and you do not have to manipulate the sorting algorithm yourself, Java.util.Collections provides a sort () method. Because an index is required to sort, the collections sort () method accepts the list action object.
20145207 Java Programming 5th Week of study summary