Learn Java Notes (4)

Source: Internet
Author: User
Tags object class definition constructor error handling execution string variable throwable
Notes

91, Package Name: lowercase letters, the use of the order of the reverse domain name.
92, the class in the package needs to be declared public, otherwise this package does not belong to the method can not use it.
93, the use of jar files to facilitate maintenance and installation, a jar file often contains more than one package, Sun J2SE provided by the standard classes placed in the Rt.jar


94. J2SE provides tool software jar to generate JAR file:
Jar CVF <jar file> <class file>
Jar CMVF <manifest> <jar file> <class file> include a customized description (manifest) file with M

MANIFEST.MF.
95, will create the package input in: import com.mycompany.shape.*; non-import com.mycompany.shp;

This jar file is included in the classpath and needs to be set in Windows: Set classpath=.; Shapr.jar. Or you can use: Java

-classpath <class path> <class file>
96, because the variables associated with the object belong to the reference type, using obj1=obj2;obj1 just get a copy of the OBJ2 application, not

Obj2 the object itself.
97, using Clone () to copy an object, Clone () inherits from the object class. Obj1=obj2.clone (); Obj2 must be replicable


98. In order for a user-defined class object to be replicated, you must use the interface cloneable and declare a public method clone ()

To perform the replication work.
99, to complete the deep copy (deep copy) (containing the referenced object and its associated objects), you need to base on the class relationship between the objects

Duplicate it on a layer by layer.
100, reflection also known as introspection, because it allows the implementation of the Java program to perform self-examination, you can get an object

The members of the owning class definition and related data, which is a Java-specific feature.
101, reflection improve functions like function pointers, can be dynamically improved when the program executes so the class object method name that executes

The parameter that you want to enter.
102, first get a class object, there are two ways
A class object, such as Obj.getclass (), is obtained by using the GetClass () by the existing class object.
Using Class.forName (String className)
103, second, using the method provided by class to get the relevant data of the class
String GetName (); Used to get the name of the class
Method[] Getdeclaredmethods (); Get all the method names declared within the class
Method[] GetMethods (); Gets all the method names in the class, including methods inherited by the parent class
Method GetMethod (String methodname, class[] parametertypes); Get a Method object
104. Finally, use methods provided by method to perform the method you want to execute
Object Invoke (object methodobj,object[] parameters); When an error occurs, the build

Invocatiotargetexception
105, using the reflection function to execute the method within the class, the input data type of the method is more special place, the syntax is:

Typename.class, such as int[] for Int[].class,int int.class,string for String.class. The parameter you entered must be a pair of

Like (Object)
106, in order to know the execution efficiency of a Java program to provide a way to improve the program, Java provides the functionality of Java profiling.

Java-xrunhprof:help is used to query the options that the Java profiling is available for use. For example, to understand the execution time of various methods in a program

Ratio, you can use Java-xrunhprof:cpu=times className. will produce a java.hprof.txt file.
107. Throwable is the parent of error handling in all Java programs, there are two types of funds: Error and exception.
108. Error: Represents the unexpected errors detected by the JVM, because this is a critical error that is part of the JVM hierarchy, causing the JVM to fail

Continued, therefore, this is not captured, can not take any recovery operation, can only display error messages at most.
109. Exception: A recoverable exception, which can be captured, so that some recovery action can be taken when an exception occurs.
110. One of the exceptions: if you want to ignore exceptions that may occur in your program and leave it to other programs to handle them, you can make these exceptions straight

Returns the invoked program, and the invoked program must handle the exceptions or return them to the invoked program.
111, the exception to the treatment of two: if you want to process these exceptions directly in the program, you can use the Try...catch (... finally)

Way.
112, finally can be omitted. If used, finally the program will be executed, generally used for the final clean-up work, such as

Closes the system resources that are open.
113, the throwable provided by the method:
String ToString (): Represents the string representation of the Throwable object.
String getMessage (): Returns the error message associated with the object.
void Printstacktrace (): Displays the path that occurs for an exception build.
void Printstacktrace (PrintStream out): Output to a PrintStream data stream, which allows information

Write to a file.
void Printstacktrace (PrintWriter out): Output to a printwriter data stream.
114, Try...catch is also a section-style framework, the scope of the child variable must also follow the same rules, sometimes in order to

A catch (or finally) section can use a variable in a try section, and it needs to declare the variable outside the Try...catch section.
115. When a program needs to capture several different exceptions, special attention should be paid to the order of these exceptions. Should be placed according to class relations between classes

, the more basic classes need to be placed in the catch () section behind.
116. An exception class needs to contain at least one default constructor (used to display the default information) and a string object as a parameter

Constructor (used to display custom information provided by the user).
117. C + + can only provide byte streams, in Java, there are bytes (byte) stream and character (character) stream points.
118, the word throttling: OutputStream provides the method:
void write (int b): Writes a byte of data
void Write (byte[] buffer): Writes the data of the array buffer to the stream
void Write (byte[] buffer,int offset,int len): Number of Len bytes written, starting from Buffer[offset]

According
void Flush (): Forces the data in the buffer to be written to the stream
void Close (): Off stream
119, InputStream provides the method:
int read (): reads a byte of data, if reached the end of the file, the return value is-1
int read (byte[] buffer): reads the buffer size data, the return value is actually reads the number of bytes
int read (byte[] buffer,int offset,int len)
int available (): Returns the number of bytes available in the stream for reading
Long Skip (long N): Skips n bytes of data, returns the actual number of data skipped
void Close (): Off stream
120, character streams. The method provided in Reader and writer class is the same as the byte stream, but the byte[in the parameter should be changed to char[]




Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.