Exceiption
When using try-catch-finally, if the Catch clause is not, the exception may be lost, this is a very serious error. So, we must add a Catch (Exception EX) clause when using try-catch-finally. Another policy is to pack all methods that will throw an exception to Try-catch, and pay attention to Catch not to miss out the exception you don't want to miss. When we know that in our approach, we will handle an exception. Do we deal with it in this method or deal with some object dealing with the above object, or completely handed over the object to the above? I have no good standards now to analyze this problem.
You can use a very simple way to pack "check-type exception", making it an "non-checking exception", and you can use the getcause () method to capture and process this exception when you need to capture. The following is an example: class WrapCheckedException {void throwRuntimeException (int type) {try {switch (type) {case 0: throw new FileNotFoundException (); case 1: throw new IOException (); case 2: throw new RuntimeException ( "Where am I?"); default: return;}} catch (Exception e) {// Adapt to unchecked: throw new RuntimeException (e);}}} class SomeOtherException extends Exception {} public class TurnOffChecking {private static Test monitor = new Test (); public static void main (String [] args) {WrapCheckedException wce = new WrapCheckedException (); // You can call f () without a try block, and let // RuntimeExceptions go out of the method: wce.throwRuntimeException (3); // Or you can choose to catch Exceptions: for (int i = 0; i <4; i ) Try {IF (i <3) Wce.ThrowRuntimeException (i); else throw new someotherexception (); } Catch (SomeOtherException e) {System.out.println ( "SomeOtherException:" e);} catch (RuntimeException re) {try {throw re.getCause ();} catch (FileNotFoundException e) {System.out.println ( FilenotFoundException: " E);} catch (ioException e) {system.out.println (" IOEXCEPTION: E);} catch (throwable e) {system.out.println ("throwable:" e); }} Monitor.expect (new string "{" filenotfoundexception: " " java.io.filenotfoundexception "," ioException: java.io.ioException "
, "Throwable: java.lang.RuntimeException: Where am i?", "SomeoTherexception: Some PortHERException"});}} ///: ~ Finally
Don't use Finally. We can't make it possible to make a cleanup work, or we can't determine that Finally will be called. For example, I will open a file now, then one operation is to read a piece of content from the file. This read content can be carried out continuously, and the direct users don't want to use it. So, we can't know what the user needs to close the file. At this time, don't put the actions close to the FINALLY to execute.
Loop
A frequent logical error. Here Is A Common Mistake People Make WHEN THEY Trying to do Nested Iteration over Two Collections:
List suits = ...;
List ranks = ...;
List sorteddeck = new arraylist ();
// Broken - throws nosuchelementexception!
Iterator i = suits.iterator (); I.hasnext ();)
Iterator j = ranks.Itemrator (); j.hasnext ();)
sorteddeck.add (new card (i.next (), j.ness ()));
Can you spot the bug? Do not feel bad if you can not. Many expert programmers have made this mistake at one time or another. The problem is that the next method is being called too many times on the "outer" collection ( Suits). It is being called in the inner loop for Both the Outer and Inner Collections, Which Is Wrong. in Order To FIX It, You Have to Add A Variable in The Scope of The Outer Loop To Hold The Suit:
// fixed, though a bit ugly
Iterator i = suits.iterator (); I.hasnext ();) {
Suit suit = (suit) i.next ();
Iterator j = ranks.Itemrator (); j.hasnext ();)
sorteddeck.add (new card (suit, j.ness ())));
}
So What Does All this has to do with the for-each construct? It is tailor-name for nested ity! Feast your Eyes:
For (Suit Suit: Suits)
For (Rank Rank: Ranks)
SortedDeck.Add (New Card (Suit, Rank));
Array
A one-dimensional array is called a clone () method is deep copy; the multi-dimensional array is called the clone () method is shallow copy. Assuming that the Class B is a subclass of a class A, V is a Type A group. v can save B instance. But after saving B, V [N] will be a B type handle, when V [N] = new a (); an operation period is abnormal, but v = new a [n]; or correct Operation. Because, V remains or A, only V [n] type changes. Nesting Interfaces
When implementing an interface, it is not necessary to achieve the interface inside. Private interfaces are only implemented in the defined classes. The interface in the interface can not be private.
Unintended Recursion
Inside the toString () method, this is often easy to generate a recursive infinite loop. Such as: public string toString () {return "InfiniteRecursion address:" this "/ n";} Call yourself in any method also generate a recursive infinite loop. Such as: void fo () {this.fo ();
Inner Classes
Other classes cannot create an instance of an internal class in addition to the subcatenaries of the host class of the internal class. If an internal class is already private, even if its subclass is not able to create an instance of this internal class. For example, if B is the internal class of A, C inherits A, D does not inherit A. Then we can create an instance of internal class B in C: AB B = new ab () cannot do this in D, in which cases to create internal class B, may be: a a = new a (); AB B = a.new b (); Access scope of the internal class (including Local Inner Classes partial internal classes and Anonymous Inner Classes anonymous internal classes) except the area of the Local Inner Classes is limited to its scope. Such as IF () {class a {}}, this A class can only be accessed within the IF block. The only reason for using local internal classes without using anonymous internal classes is: You need to create multiple types of objects. Inside the same name in different classes in the same place can be used in the same name. Inside the A and B can be used in internal class C, they do not conflict. Because accessing any class categories must be connected with its external class name, such as A.c. When defining an anonymous internal class, if you still need to use the outside object, you must declare this parameter to Final. Such as: Object Test (Final String Str) {private string value = str;};} To initialize the internal class to anonymous internal classes, there are two ways: First, instantimetric objects to initialize anonymous internal classes, Such as: New Object (i) {} ;. This is initialized to Object to the Object, thereby achieving the effect of this anonymous internal class. The second is to implement in the initialization part of anonymous internal classes, but there is only one area, it is impossible to achieve reconstruction of it. That is, only this constructor is used. Such as: New Object (Final String Str) {Private String Value; {Value = Str; IF (Value.equals ("Test")) Value = Str "OK!"; Internal classes can directly access the members of his host class. This is very useful. Such as: public class test {object [] Object = new object [5]; private class innertest {public void readobject (int i) {return object [i];}}} There is no Static data in the normal internal class, Static data member And nested classes.
When inheriting internal classes, derived classes must give a host class handle in the constructor, then do a special operation, such as: Class With {class inner {}} public class inheritinner extends forinner.inner {//! Inheritinner () {} // WINNER WI) {wi.super ();} public static void main (String [] args) {WITHINNER WI = New WITHINNER (); Inheritinner II = New Inheritinner (Wi );}} ///: ~ Inherited internal class with the internal class of the parent class, does not overwrite the internal class of the parent class. They are two independent instances, they have their own namespace. As A has an internal name called C, B is a subclass of A, and B also declares that an internal class name is also called C, A is represented by AC, and C is represented by BC Their names do not have conflicts. We are asked to pass C Extends A.c in B, so B.C has become a subclass of A.c, which can meet some of our special requirements. NESTED CLASSES can create a nested object. Non-Static host class objects cannot be accessed in nested classes. Access Static data, Static data members, and nested classes. Why use internal classes
Each internal class can inherit a "Implementation" independently, so the internal class is not bound by the "host class that has inherited other implementations". Such as: class a {} abstract class b {} class c extends a {b makeb () {return new b () {};}} public class mutiimplementation {public static void HOLDA (A a a) {} public static void HoldB B b) {} public static void main (string [] args) {c = new c (); HOLDA (C); HoldB (c.makeb ());}} The internal class can have multiple instances, and each It can be its own, state information that is independent of the host object. One host class can put several internal classes, and each internal class can achieve the same interface in different ways, or the same class. The time created by the internal class object is not subject to the creation of the homing class object. The internal class does not have "IS-A" relationship. It is an independent entity. For example: if there is no internal class, we use it to implement a Selector interface, then you can only say "Sequence is a selector", so there is only one Selector in each sequence. But with an internal class, you can define a method getrselector (), let it return to a Selector who has visited this sequence. The internal class provides this flexibility.