Study number 20145336 "Java Program Design" 5th Week study summary textbook Learning content Summary
Try Catch
- The JVM tries to execute the contents of the try block first, and executes the code in the catch block if an error occurs and matches the type following the catch.
- To recover a program's normal execution after catching (catch) errors, you can put the try, catch chunks into the while loop.
- Since the JVM is trying to execute a try block, once an error is captured, this time the try block is equivalent to no execution.
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:
Package ch08;Import Java.io.FileInputStream;Import java.io.FileNotFoundException;Import Java.util.Scanner;public class fileutil {public Span class= "Hljs-keyword" >static string readFile (String Name) throws FileNotFoundException {StringBuilder text = new StringBuilder (); try {Scanner console = new Scanner (new FileInputStream (name)); while (console.hasnext ()) {text.append (console.nextline ()). Append (catch (FileNotFoundException ex) {ex.printstacktrace (); throw ex;} return text.tostring ();}}
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.
Understanding the collection Architecture
There is often a need to collect objects in the program, so far, you've learned that the way you can collect objects is to use an object array, and in javase you actually provide several classes of objects to collect. Instead of re-creating similar APIs, you can take these classes directly. The behavior of the collection object, like the Add () method of the new object, removes the object's remove () method, and so on, is defined in java.util.Collection. Since the object can be collected, you can also get the object, this is the behavior defined by java.lang.Iterable, it defines the iterator () method to return the Java.util.Iterator operand, you can get the object. However, the collection object will have different requirements, if you want to record the index order of each object at the time of collection, and can retrieve the object by index, such behavior is defined in the Java.util.List interface. If you want to collect objects that are not duplicated and have the behavior of the collection, it is defined by Java.util.Set. If you want to collect objects, arrange them in a queue. The collected objects are added to the tail end, and when the object is obtained from the front end, Java.util.Queue can be used. You can use Java.util.Deque if you want to add, remove, and move the ends of a queue.
The commonly used map operation class is java.util.HashMap
with java.util.TreeMap
, and it inherits from the abstract class Java.util.AbstractMap.
- Use HashMap: After the key values are established in the HashMap, the keys are unordered.
- Use TreeMap: If you use TREEMAP to establish a key value corresponding, the part of the key will be sorted if the object that is the key must operate the comparable interface, or an object that specifies the comparator interface of the operation when the TreeMap is created.
- Using the Properties:properties class to inherit from the hashtable,hashtable operation of the map interface, properties naturally also have map behavior. Properties = Left Set property name, right set property value. The InputStream instance can be specified using the properties's load () method. The InputStream instance is automatically closed when the load () method finishes.
If you want to get all the keys in the map, you can call the map's keyset () to return the set object. If you want to get the key and value of the map at the same time, you can use the entrySet()
method, which will return a set object, each element is an Map.Entry
instance, you can call getKey()
the Get key, call to getValue()
get the value.
List with index
ArrayList
Generally used in linear structures.
LinkedList
Generally used when linking structures.
- The set, which does not duplicate content as the name implies, is not duplicated, and set is an interface that is used to find duplicates.
Problems in teaching materials learning and the solving process
This week's study is still a lot of shortcomings of the place there are many needs to improve, learning the code exception processing, later encountered problems can be more convenient to improve.
Learning progress Bar
| | Lines of code (new/cumulative) | Blog volume (Add/accumulate) | Learning time (NEW/cumulative) | Important growth |
| -------- | :----------------:|:----------------:|:---------------: |:-----:|
| Target | 5000 Rows | 30 Articles | 400 hours | |
| First week | 30/30 | 2/2 | 20/20 | |
| Second week | 100/130 | 2/4 | 18/38 | |
| Third week | 100/230 | 3/7 | 22/60 | |
| Week Four | 300/530 | 2/9 | 30/90 | |
Resources
- Java Learning Notes (8th Edition)
- Java Learning Note (8th Edition) Learning Guide
- ...
Java 5th Chapter Study Summary