Abnormal
Exception: In Java refers to an object that is thrown by a method.
Category: Check for exceptions, run-time exceptions, errors
Run-time Exception (UNCHECKD): RuntimeException and its subclasses
Check for exceptions (checkd/): Refers to exception and its subclasses, and is not a subclass of runtimeexception; it must be processed to find
Grammar:
Try { // protected code }catch(exception name E1) { // capture block }
Note: Special recapture generic exceptions should be captured first, and the try does not show an exception catch will not execute
Three options for exception handling:
1) Catch this exception and not let him continue to throw down the call stack
2) Catch this exception and continue to throw down
3) does not catch this exception, causing the method call stack to be ejected, causing the object to be thrown to the main () method below the call stack.
Declaring exceptions:
Keyword:throws used when a method does not handle check exceptions
For example: public void deposit (double amount) throws runtimeexception{}
To throw an active exception:
Keyword:throw
Run-time exceptions can not declare exceptions, check exceptions must be declared
Final Keyword:
Used to create a block of code after a try block, and he always runs, regardless of whether the exception occurred
Note: Before the method returns, thefinal block will execute
User-defined exceptions:
1) All exceptions must be sub-classes of Throwable
2) If you want to write a check exception for a rule that will be automatically handled by an exception, you need to inherit the exception class;
3) If you write a run-time exception, you need to inherit the RuntimeException class
Common classes
Classes that run platform interaction:
System class: record execution time, copy array, determine current system properties and get get system environment variables, etc.
Runtime class: The object can be obtained by his static method GetRuntime ()
String class:
StringBuffer class: A variable-tolerant string representing a class (method is thread-safe)
StringBuilder class: Representing a string object with slightly higher performance
Date class:
DateFormat Abstract Class: Method returns a DateFormat object in a specific region format
SimpleDateFormat class: When you create an object, you need to pass in the date and time format as a formatted template
Calendar abstract class
GregorianCalendar class: Gregorian calendar
Random number:
Format example: Generate a random number of 0-10
Random r=New random (); int n1=r.nextint (); N1=math.abs (R.nextint ()%10); SYSTEM.OUT.PRINTLN (N1);
Boxing: Basic type to reference type
Unboxing: Reference type to base type
Regular Expressions:
\; ^ (start); $ (end); * (0 or more); + (1 times or more); (0 times or once); {n} (repeats n times); {n,m} (up to M min N);. (any single character outside of \ n); x|y (x or Y);
[XYZ] [^XYZ] (any character not included), [A-z] (range), [^a-z] (not in range), \b (word boundary), \b (non-word boundary), \d (a numeric character), \d (not a number);
\f (page change), \ n (line break), \ r (carriage return), \s (blank), \s (non-blank), \w (underscore and any character); \w (non-word character);
Correct use:
Pattern class: Compile regular expression, create object, first tune the static method compile () returns a Pattern object
Matcher class: Parse pattern, get object, call Matcher () method of pattern object
Formats such as:
String name= "y_123456"; Pattern P=pattern. Compile (regular character); Matcher m=p.matcher (name); if (Matcher.matches ()) {} Else {}
Reflection
The supplied loader is included in the Jvam:
1) Root class loader: Load core Java Class (jiava.* start)
2) Extension ClassLoader: Loading the Extended Java class (Java.* the jar in the beginning of the package)
3) System loader: Load its own class
The loading mechanism of the class:
1) After the class file is read into memory, a Java.lang.util.class object is created, one of which is loaded into the JVM, and the same class is not re-entered
2) Connection: Merges the binary system data of the class into the JRE
3) Initialization
Reflection:
Meaning: You can load, detect, and use classes that are completely unknown during the run time
Load: Class C=class.forname ("Fully qualified class name");
See All methods:
Method[] motheds=cl.getdeclaredmethods (); for (Method me:motheds) { System.out.println (Me.getname ()); }
To view the constructor:
Constructor[] co=cl.getdeclaredconstructors (); for (constructor<?> Con:co) { System.out.println (con); }
Get field:
field[] fields = clazz.getdeclaredfields (); for (Field field:fields) { + "" + field.getname ()); }
To create an object:
1 constructor<?> Constructor = Cl.getconstructor (String. Class, Date. class ); 2 SimpleDateFormat s=New SimpleDateFormat ("Yyyy-mm-dd"); 3 String st= "2000-10-12"; 4 Date da=S.parse (ST); 5 Object obj = constructor.newinstance ("Zhang San", DA);
Call Method:
1 Method setaddress = Cl.getmethod ("Setaddress", String. Class); 2 Object adress = setaddress.invoke (obj, "Chengdu"); 3 Method Setsex=cl.getmethod ("Setsex", String. Class); 4 Object Sex=setsex.invoke (obj, "male");
... Represents a mutable parameter
Java Collection Framework
Collection Interface (control single-column data):
Divided into two sub-interfaces:
Set Interface (no order and cannot be duplicated):
Two important implementation classes:
HashSet: set interface implementation based on hash algorithm, no order, duplicate element not allowed
Threeset: in order
List Interface (sequential, can be repeated)
Two important implementation classes:
ArrayList: is implemented in array mode, the query efficiency is high, the insertion and deletion efficiency is low
LinkedList: is implemented in a linked list, suitable for frequent addition of deleted elements (fast speed)
Important methods:
Insert: Object name. Add (); Returns the specified: Object name. get (); Replace: Object name. Set (); Delete: Object name. Remove ();
Generic type:
Effect: 1) Ensure element type safety in the set; 2) data type no forced conversions
Essence: Allows specifying type parameters when defining interfaces, classes
For example:arraylist<integer> al=new arraylist<integer>;
Interator Interface: An interface for traversing the collection, with a iterator method that returns the Interator object
This interface method:
More elements: Hasnaxt (); Next element: Nest (); Delete the last element returned by iterator: Remove ();
Format:
1 Public Static voidMain (string[] args) {2Mycomparator my=Newmycomparator ();3List<sortobject> l=NewArraylist<sortobject>(); 4Sortobject s1=NewSortobject (1,4);5Sortobject s2=NewSortobject (7,9);6Sortobject s3=NewSortobject (5,6);7 L.add (S1);8 l.add (S2);9 L.add (S3); Ten Collections.sort (l,my); OneIterator it =l.iterator (); A while(It.hasnext ()) { -Object obj =It.next (); - System.out.println (obj); the } - - } - +}
Map Interface (Control key value pair):
Meaning: Corresponds to a set of correspondence between a key (key) and a value (value).
Two implementation classes: HASHMAP; TreeMap;
Basic methods:
Returns the value associated with the specified key: Get (); Add a key-value pair to the map: put (); Delete all elements: Clear (); Delete specified: Remove ();
Auxiliary classes Collections and Arrays
collections class : used to sort, query, modify a collection element (static method)
For example sort: collections.sort (list cmp);
Arrays class : manipulating arrays
For example: Arrays.sort (arr);
Gets the array length with size in the collection
Java exception handling, common classes, Reflections, collections