Chapter 9, violation error control
The basic principle of Java is "The code wrong code will not run". <1> In Java, for the client programmer to call the method, we must notify them that they may "throw" violations from their own methods. This is a polite approach, only it can enable customer programmers to accurately know what code to write to capture all potential violations. Of course, if you provide the source code, the customer programmer can even check the code, find the corresponding throw statement. But in this way, it is usually not provided with the same source code. To solve this problem, Java provides a special grammatical format (and forced us to adopt) to politely tell the customer to "throw" what violations of "throw", so that the other party is conveniently controlled. This is the "violation specification" we have to tell here, which belongs to a part of the method declared, which is located behind the list of auto-variables (parameters). The violation specification uses an extra keyword: throws; follows all the potential violation types. Therefore, our method definition looks like this: void f () throws tool, toosmall, divzero {// ...}
<2> Capture all violations We create a controller to capture all types of violations. The specific approach is to capture the basic type of violation type Exception (there are other types of basic violations, but Exception is the basis for almost all programming activities). As shown below: Catch (Exception E) {system.out.println ("CAUGHT An Exception");} This code can capture any violations, so it is best to place it at the end of the list of controller during actual use, prevent Follow any special violation controllers behind the latter failure. For all violations commonly used by programmers, because the Exception class is their foundation, we will not get information about violations, but you can adjust the basic class throwable from it: String getMessage () get detailed news. String toString () Returns a brief description of the Throwable, including detailed messages (if any).
<3> Standard Java violation Java contains a class named throwable, which is described in all things that can be "throw". Throwable objects have two conventional types (ie "from throwable inheritance"). Among them, ERROR represents compile periods and system errors, we generally do not have to deliberate them (except for special circumstances). Exception is the basic type of "throw" from the class method of any standard Java library. In addition, they can also "throw" from our own approach and running period.
<4> Violation of violations Use violations to do this: (1) Solve the problem and call the way to cause violations. (2) The development of calibration, and continue without reseeding the method. (3) Calculate the other results, not the result of the desired method. (4) Solving the problem as much as possible in the current environment, and re-"throwing" out of the same violation. (5) Solving the problem as much as possible in the current environment, and re-"throwing" out of a more advanced environment. (6) The abort process is executed. (7) Simplify the encoding. If the violation program makes things more complicated, it will be very troublesome, it is better not. (8) Be safer to make your own libraries and procedures. This is a "short-term investment" (easy to debug), is also a "long-term investment" (to improve the robustness of the application)
Chapter 10 Java IO System
"For language designers, create a good input, the output system is a particularly difficult task." Due to a large number of different design, the difficulty of this task is easy to prove. The biggest challenge seems to be how to overwrite all possible factors. Not only do three different types of IO need to consider (files, consisters, network connections), but also communicate with them through a large number of different ways (order, random access, binary, characters, line, feature, etc.). You can split the IO class of the Java library into inputs and outputs. By inheriting, all classes derived from inputStream have a basic approach called read () to read a single byte or byte array. Similarly, all classes derived from OutputStream have basic methods Write () for writing a single byte or byte array. However, we usually do not use these methods; they exist because more complex classes can take advantage of them to provide a more useful interface. We feel that the stream library of Java is extremely complex, which is because in order to create a single result stream, you need to create multiple objects. It is necessary to classify the class according to the function. The instrument of the library first decides all classes related to the input from inputStream, while all classes related to the output are inherited from OutputStream.
/ / Write a class of classic read and write files, implement a counter in a file manner
Import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.fileReader; Import java.io.filewriter; import java.io.printwriter;
/ ** * @Author GMS * Create 2005-1-18 14:39:24 * /
Public class filew {private file f = new file ("d: //counter.txt"); public int getNum () {INT i = -1; try {string stri = ""; bufferedreader in = New BufferedReader (New FileReader) (f)); while ((stri = in.readLine ())! = null) {// Less by line read i = integer.parseint (stri.trim ());} in.close ();} catch Exception e) {E.PrintStackTrace ();} return i;} public void setnum () {INT i = getnum (); i ; try {printwriter out = new brewritedwriter (New Filewriter (New Filewriter (F, False))) Out.write (String.Valueof (i)); / / may be the reason for the encoding, if it is written directly to int, the Java coding and Windows encoding will appear, so it is written here that string out.close ( );} Catch (exception e) {E.PrintStackTrace ();}} public static void main (String [] args) {filew frW = new filew (); for (int i = 0; i <9; i ) { Frw.setnum (); s YSTEM.Out.println (frw.getnum ());}}} Chapter 11 Operation Type Identification
The concept of running type identification (RTTI) is very simple - when there is only one handle of the base type, use it to determine the correct type of an object. However, the need for RTTI needs to expose many interesting (and often confusing) issues, and officially put the program's texture problem. This chapter will discuss how to find objects during operation and Class information. This mainly takes two forms: one is "traditional" RTTI, which assumes that we have all types we have in compilation and running period; the other is the "reflection" mechanism for Java1.1, and uses it to find independently at runtime Class information. First discuss the "traditional" RTTI and discuss the reflex problem.
The RTTI forms we are known include: (1) Classic modeling, such as "Shape", use RTTI to ensure the correctness of the shape, and generate a ClassCastException violation after encountering a failure. (2) Represents the Class object of the object type. You can query the Class object to get useful running period.
Chapter 12 Pass and return to objects
So far, the reader has a more profound understanding of the "delivery" of the object, remember that actual transmission is just a handle. So generally asked: "Is Java pointer?" Some people think that the operation of the pointer is very difficult, and it is very dangerous, so a wishful thinking is not good. At the same time, because Java has such a good reputation, it should be easily exempt from the trouble of our previous programming, which is not likely to have a "dangerous goods" such as a pointer. However, it is precisely that Java has a pointer! In fact, the identifiers per object in Java (except for the basic data type) are one of the pointers. However, their use has been strictly restricted and prevention, not only the compiler has "Coheli" for them, but the runtime system is no exception. [Slightly summarize] All arguments or parameter passes in Java are performed by passing the handle. That is, when we pass the "an object", actually passed only "a handle" of the object located outside the method. So once you want to make any modifications to that handle, it is equivalent to modifying the external object. Also: ■ Automatically generate alias issues during parameter delivery ■ There is no local object, only local handles ■ The handle has its own scope, and the object is not ■ "Existing time" in Java is not a problem ■ There is no language Support (such as constants) prevents objects from being modified (to avoid aliasing side effects), if only the information is read from the object, the password is the most efficient form of the self-variable transmission. This is very appropriate; the default method is generally the most effective way. However, it is sometimes necessary to treat the object as "local", so that the change we make only affects a local copy and does not affect the outside object. Many programming languages support a local copy (notes 1) that automatically generates external objects within the method. Although Java does not have this ability, we allow us to achieve the same effect.
1: In the C language, a small amount of data bits are usually controlled, and the default operation is passed by value. C must also follow this form, but the value of the value is not certain to be an effective way. In addition, the code used to support the value transfer in C is also difficult to write, and it is a headache.
Chapter 13 Creating Windows and SMDs
slightly
2005-3-11