The basic principle of Java is that "Malformed code does not run". <1> in Java, for client programmers who want to invoke methods, we want to inform them that they may "throw" the offending in their own way. This is a polite way to make it possible for the client programmer to know exactly what code to write to capture all potential violations. Of course, if you also provide the source code, the client programmer can even check the codes to find the appropriate throw statements. However, it is not usually accompanied by a source repository. To solve this problem, Java provides a special syntax format (and forces us to adopt) to politely tell the client programmer that the method will "throw" the offending and make it easy for the other person to control. That's what we're going to talk about here, "the offending code," which is part of the method declaration, which is behind the argument list. The offending specification uses an additional keyword: throws followed by all potential types of violations. Therefore, our method definition should look like this: void F () throws Toobig, Toosmall, Divzero {//...}
<2> capture all violations we can create a controller that captures all types of violations. The specific approach is to capture the underlying class violation type exception (there are other types of underlying violations, but exception is the basis for almost all programming activities). as follows: catch (Exception e) {System.out.println ("caught an Exception");} This code captures any violation, so it is best to put it at the end of the list of controllers when it is actually used, to prevent any special unauthorized controller following it from failing. For all the offending classes that programmers often use, because the exception class is their basis, we don't get much information about the violation, but we can call the method from its underlying class Throwable: String GetMessage () Gets the detailed message. String toString () returns a brief description of the Throwable, including detailed messages, if any.
<3> Standard Java offending Java contains a class called Throwable that describes everything that can be "tossed" as an offence. There are two general types of Throwable objects (i.e., "Inherit from Throwable"). Where the error represents compile time and system errors, we generally do not have to deliberately capture them (except in exceptional cases). Exception is the basic type that can be "tossed" from a class method in any standard Java library. In addition, they can be "tossed" from our own methods and from the occasional events of the run period.
The <4> offences do the following: (1) solve the problem and invoke the method of causing the offence again. (2) To quell the development of the situation and to continue without trying the method again. (3) Calculating other results rather than the results of the desired method. (4) To solve the problem as much as possible in the current environment, and to "throw" the same violation back into a more advanced environment. (5) To solve the problem as much as possible in the current environment and to "throw" different offences back into a more advanced environment. (6) Suspending the execution of the procedure. (7) Simplified coding. If the offending scheme makes things more complicated, it will be very annoying. (8) Make your own libraries and programs more secure. This is both a "short-term investment" (easy to debug) and a "long-term investment" (improving the robustness of the application)
10th Chapter Java IO System
"For language designers, creating a good input and output system is a particularly difficult task." "Because there are a lot of different designs, the difficulty of this task is easy to prove," he said. One of the biggest challenges seems to be how to cover all possible factors. Not only are there three different kinds of IO to consider (files, consoles, network connections), but they need to be communicated to them in a number of different ways (order, random access, binary, character, by line, Word, and so on).
The IO class of the Java library can be divided into two parts of input and output. By inheritance, all classes derived from InputStream (input stream) have a basic method named Read (), which is used to read a single byte or array of bytes. Similarly, all classes derived from OutputStream have the basic method write (), which is used to write a single byte or byte array. However, we don't usually use these methods; they exist because more complex classes can take advantage of them to provide a more useful interface. We feel that Java's stream library is unusually complex because of the need to create multiple objects in order to create a single result stream. It is necessary to categorize classes by function. The designer of the library first decides that all classes related to the input inherit from InputStream, while all the classes associated with the output inherit from OutputStream.
Here to write a more classic read and write file class, the way to implement a file counter
public class Filerw { 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) { //row-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 PrintWriter (new BufferedWriter (New FileWriter (F,false)); Out.write (string.valueof (i)); //May be the reason for encoding, if you write int directly, there will be Java coding and Windows coding chaos, So the writing here is string out.close (); }catch (Exception e) { E.printstackTrace (); } } public static void main ( String[] args) { filerw frw = new Filerw (); for (int i =0;i< 9; i++) { Frw.setnum (); System.out.println (Frw.getNum () ); } }}
The 11th chapter of the operational period type identification
The concept of runtime type identification (RTTI) is very simple--use it to determine the correct type of an object when there is only one handle to the underlying type. However, the need for Rtti exposes many interesting (and often confusing) problems with object-oriented design, and formally puts the structure of the program on the table. This chapter discusses how to use Java to find objects and class information during run time. This takes two forms: a "traditional" RTTI, which assumes that we have all the types at compile and run time, and a Java1.1 unique "reflection" mechanism that allows you to find class information independently at run time. First we discuss the rtti of "tradition" and then discuss the reflection problem.
Our known Rtti forms include: (1) Classic styling, such as "(Shape)", which uses RTTI to ensure the correctness of the modelling and creates a classcastexception violation after encountering a failed shape. (2) A class object that represents the object type. You can query the class object to get useful run-time data.
Chapter 12th Transfer and return of objects
So far, readers should have a more profound understanding of the "transmission" of objects, remembering that the actual passing is only a handle. Then generally ask: "Java has pointers?" "Some people think that the operation of the pointer is very difficult and very dangerous, so wishful thinking that it is not good." At the same time because Java has such a good reputation, so it should be easy to forgive their previous programming in the trouble, it is impossible to carry a pointer such as "dangerous goods." But, to be precise, Java is a pointer! In fact, identifiers for each object in Java (except the basic data type) belong to one of the pointers. However, their use is strictly restricted and guarded, not only the compiler is "wary" of them, the runtime system is no exception.
"A little summary" all of the arguments or parameter passes in Java are passed through the handle. That is, when we pass "an object," the actual pass is just a "handle" to the object that is outside the method. So once you make any changes to that handle, it's equivalent to modifying the external object. In addition: an alias is generated automatically during parameter passing the local object is not present, only the local handle handle has its own scope, and the object does not have the "time" of the object It's not a problem in Java. There is no language support (as usual) to prevent objects from being modified (to avoid the side effects of aliases) if you simply read the information from the object without modifying it, passing the handle is the most efficient form of the argument passing. This is the right thing to do; The default method is generally the most effective method. However, it is sometimes still necessary to treat objects as "local", so that the changes we make affect only a local copy and do not affect the objects outside. Many programming languages support the automatic generation of a local copy of an external object (comment ①) within a method. Although Java does not have this capability, it allows us to achieve the same effect.
①: In C, a small number of data bits are usually controlled, and the default action is passed by value. C + + must also follow this form, but passing objects by value is not necessarily an effective way. In addition, the code used in C + + to support passing by value is difficult to write, and it is a headache.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.