The eighth chapter of textbook Learning content
-
Java exception handling is to handle the Exception class and its subclasses (Checked Exception), and RuntimeException and its subclasses are not processed (unchecked Exception)
-
Multi-Catch: Cannot have an inheritance relationship between classes
-
Catch or throw?
Throws: You cannot handle it, you throw checked Exception in the method declaration, there must be throws in the method declaration.
-
Try, catch syntax
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 matches the type that is declared in the catch bracket, conforms to the thrown Error object type, and executes the program code in the catch block if it is.
No matter if there is an exception in the try block, if a finally block is written, the finally block is bound to be executed, and if the program writing process returns first, and there is a finally chunk, finally the block will be executed before the value is returned.
Application Examples:
Import java.util.*;
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);}
}
- Throwable
Throwable defines methods for getting error messages, stack traces, and so on, which have two subclasses: Java.lang.Error and Java.lang.Exception.
Nineth Chapter
-
Collects the object's behavior, like the Add () method of the new object, removes the object's remove () method, and so on, are defined in Java.util.Collection, java.lang.Iterable defines the iterator () method to return the Java.util.Iterator operand, which allows you to get the collected objects one after the other.
-
In Java, the Hashcode () and the Equals () method are called when the object is judged to be duplicated, and two methods must be manipulated simultaneously.
-If the collection object often has changes in the index, consider the connection operation of the list will be better.
- SetProperty () of the general properties specifies the key value of the string type, GetProperty () specifies the value of the string type, often referred to as the property name and the property value.
-map does not inherit iterable, there is a foreach () method that is defined on the map interface, which can be used in conjunction with lambda expressions to achieve good readability when iterating over keys and values.
Application Examples:
import java.util.*;public class Messages3 {public static void main(String[] args) { Map<String, String> messages = new TreeMap<>((s1, s2) -> -s1.compareTo(s2)); messages.put("Justin", "Hello!Justin讯息!"); messages.put("Monica", "给Monica的悄悄话!"); messages.put("Irene", "Irene的可爱猫喵喵叫!"); System.out.println(messages);}
}
Problems in teaching materials learning and the solving process
Question: What is the concept of a map key, a good abstraction
Resolution: a=100a: Key 100: Value should be called: Key value pair. In addition, the key values in the map can be of any type.
Map Get key value
Map stores data in the form of key/numeric pairs, and arrays are very similar to the indexes that exist in the array, which are themselves objects.
The interface of Map
map---Implementation Map
The inner class of the map.entry--map that describes the key/value pairs in the map.
SortedMap---Extend the map to keep the keys in ascending order
Other (sentiment, thinking, etc., optional)
Now I am still in the early stage of learning Java, more important is to learn from good code to find the feeling, especially in the code to knock on the book with the head, not only to complete the task is not careful to make mistakes or not to think, hand brain and use to have a good effect.
Summary of the 5th week of Java programming