Java review notes, java Review

Source: Internet
Author: User

Java review notes, java Review

I recently want to build two JavaWeb-based network systems. I think of my poor Java skills... Teardown! So it took three days to review the important knowledge of java, mark a note.

Directory

I. Basic Knowledge

Ii. string

Iii. Category

Iv. Management

V. Generic

6. threads

7. Reflection

 

I. Basic Knowledge

1. Java SE can be divided into four main parts: JVM, JRE, JDK, and Java.

2.

The string is not only an array of characters, but also an instance of the string class.

2. Static decomposition methods: parseByte (), parseShort (). parseInt (). parseLong (), parseFloat (), parseDouble ()

=> NumberFormatException

3. Use indexes to retrieve strings

4. The string content is immutable (not the original character object)

5. A string pool will be maintained during Java execution ). For example, string str = "aa"; str0 = "aa"; str = str0 (referenced from the same object)

Intern (): First equals () the content in the pool. if the content is the same, the reference of the object in the pool is returned.

Eg: String str1 = new String ("aa"); String str2 = new String ("aa"); str1.equals (str2 );

A total of three string instances are generated ." Aa "itself, exists in the pool, and new two String objects are referenced by str1 and str2 respectively.

6. StringBuilder class. The default value is 16 characters. The length is automatically added to the extended characters.

StringBuilder

StringBuffer synchronization and Multithreading

7. split ()

 

8. Regular Expression

Operating principle:

The static method of Matches () pattern. The return value is boolean (), indicating whether the string conforms to the regular expression.

Repeat the regular expression as an object and compile it using the static pattern method compile () to return an instance of pattern. Then, you can repeat the matcher () method of the instance.

Pattern pattern = Pattern. compile ();

Matcher matcher = pattern. matcher ();

Show matcher. group ()

Iii. Category

 

 

(1) methods that can meet the number and type of parameters before packing

 

(2) A method that can meet the number and type of parameters after packing

 

(3) method eg: int sum (int... nums) {for (int sum: nums) {}}. The actual input parameter is int [] nums.

 

(4) the corresponding method cannot be found and an error is reported.

 

9. Garbage Collection

 

10. The extends public member can be called directly in the subclass.

11. After extending a class, you can use the constructor of the supeer () base class. In the constructor of the subclass, if super () is not used, the non-argument constructor of the parent class is called by default.

 

12. The protected derived class (subclass) can directly access members in the base class without being obtained by external objects.

13. The toString () of the object returns the class name and hexadecimal encoding by default: getClass (). getName () + '@' + Interger. toHexString (hashCode ()). Can be rewritten.

Equals () is used to compare whether the object's memory address is the same. HashCode () needs to be rewritten simultaneously when you override equals (). In the related environment based on the hash code, when we need to compare whether two objects are the same, in addition to using equals (), we also use the hashCode () method.

14. clone () method to copy the object itself. To support copying its own objects, you must implement the Cloneable interface when defining the class. Otherwise, CloneNotSupportedException will be thrown when the clone () method of its instance is called.

15. Polymorphism: Common Implementation interfaces (usually public methods defined on classes) are used to implement different object instances. To reduce the dependence on the Implemented interfaces. Java allows programs to depend on abstract classes or interfaces when implementing polymorphism. Abstract extends

The purpose of an interface is to define a set of methods that can be implemented. The class that implements an interface must implement all the methods of this interface, as long as the object implements an interface, you can use this interface to implement the corresponding methods on the object. Interface implement

 

Every time an interface is implemented, one more implementation protocol is followed. A class can only inherit one parent class at a time, but one interface can inherit multiple parent interfaces at a time. (Defined starting with I)

 

Iv. Management

1. Related: static factory, Iterator

2. A. java file can only contain one public class. classes not declared as public can only be called by instances of classes in the same package. If you do not use public when declaring a class, the default value is the package access range.

3. Exception: indicates an incorrect object in Java. It is best to use only for error handling, not for part of the business logic, because exceptions consume resources.

Try (unique) must have catch (not necessarily unique) and finally (unique) at least one.

A method declaration of the parent class throws an exception. If this method is rewritten, only some exceptions thrown in the parent class are thrown and child classes of the parent class are thrown. You cannot throw other undefined exceptions in the parent class method or the parent class that throws an exception.

4. Assertion the program is expected to be in a certain state. By default, this parameter is not checked at startup unless the-enableassertions or-ea independent variable is used.

Assert boolean_expression; false throws java. lang. AssertionError

Assert boolean_expression: detail_expression; false: detail_expression

5. The Enumeration type is a special type of class member, which is modified as final public static by default. Non-public constructors can be constructed (Applications in Singleton mode)

 

V. Generic

1. Before J2SE5.0, use the Object to define the class and convert it to the original type or an appropriate interface.

With generics, use <T> to declare the name of a type owner. When declaring and configuring objects using classes defined by generics, you can use angle brackets to specify the True Type of the wildcard class holder T without type or Interface Conversion.

2. You can use generics to declare an array, but you cannot use generics to create an array instance.

3. Restrict the generic available type and use extends to specify the type owner for instantiation. The instantiated object must be extended from a certain type or implement an interface.

4. Type Wildcard (Wildcard )? Represents the unknown type and is limited by the extends keyword (New types cannot be added, but can only be retrieved or removed)

<? Extends someClass> the following limits can only be contained, or subclass

<? Super someClass> the upper limit can only be contained, or the superclass

5. Expand generic classes and implement generic interfaces. It is recommended that the type owner of the parent class be retained.

 

6. threads

1. A process is a program containing its own execution address. RR rotation method. It can include multiple threads, that is, the thread execution process. Because the RR time slice is short, the program looks like performing multiple different subflows at the same time.

2. JAVA Implementation thread, can inherit java. lang. thread class, and re-define run (), sample the custom Thread class, and then start the Thread using start.

You can also implement the java. lang. Runnable interface to define classes that contain thread functions. The Runnable interface defines a run () method. When you sample a Thread object, you can pass in the object of a Runnable interface as the independent variable. The Thread object will call the run () method of the Runnable object () method, and then execute the process defined in it.

3. java can only inherit one class at a time.

4. If you want a thread to terminate after the end of the thread that generates it, set it to a Daemon thread and run the service in the background. SetDaemon () method.

5.

After you sample a Thread and execute start (), the Thread enters the Runnable state (ready for execution) and waits for scheduling. SetPriority () method sets the thread priority (1 ~ 10, the default value is 5 ). The same priority is RR.

6. For systems that do not support Timeslicing, use the yield (js analogy) to give the current thread the courtesy of other threads (pause and enter the Runnable State ).

7. Insert a high-priority (first executed) thread in the middle of join.

8. ThreadGroup thread group. Judgment: Thread. currentThread (). getThreadGroup (). getName ().

9. Handle Unchecked exception. Before J2SE5.0, use uncaughtException (), then use the Thread. UncaughtExceptionHandler interface, and implement its uncaughtException () method.

10. Synchronize synchronized. When data is shared by different threads and the same object information is updated at the same time.

11. each object is locked and the method identified as synchronized becomes the synchronization region. When the thread executes the synchronization region of an object, it needs to execute the thread in the synchronization region, you must first get the object lock. After the synchronization area is executed, the lock will be returned to the object. Because only one object is locked, when a thread removes the code locked in the execution synchronization area, other threads wishing to execute the synchronized area wait in the lock pool until they are returned. A locked thread can enter the Runnable status and wait for scheduling.

12. wait () must be in the synchronous method or block and require the thread to enter the waiting pool of the object.

Notify () from waiting for the pool to Notify a thread to the Blocked status of the lock pool, the notified thread is random and will compete with other threads for object locking. (Producer Mode)

13. The thread security issue is not considered by default in the container. You must implement synchronization on your own. You can use synchronizedXXX () of java. util. Collection to return a synchronized container object. Objects returned in this way must still be synchronized when Iterator is used to traverse objects. After J2SE 5.0, java. util. concurrent contains some classes to ensure thread security.

14. After JDK1.2, each thread can be given a specific space to keep the resources exclusive to the thread, using java. lang. ThreadLocal.

15. util. concurrent. callable and util. concurrent. future assists in the future mode. When a request occurs, a Future object is generated to the requesting customer, similar to a proxy, at the same time, the real target object of the proxy is generated continuously by a new thread.

16.

1. you can use the getClass () method of the Object to obtain the Class Object corresponding to each Object, or use the class constant to obtain the class Object, you can operate some public methods on the class object to obtain the basic information of the class.

2. Java is loaded only when classes are required. For example, declaring the reference name when new is used to generate an object does not cause the class to be loaded. A Class only has one Class instance in JVM and exists as an instance. The basic types also have corresponding Class objects.

3. You can use the static forName () method of Class to dynamically load classes when you have to specify a Class name to load classes without knowing in advance what classes the user will load. Specify the class name, or even whether the static block is run when the class is loaded, and specify the class loader.

4.

5. Run the java XXX. class command in command mode.

=> Find the JRE installation directory for the java running program

=> Find jvm. dll (default: bin \ client)

=> Start jvm and perform Initialization

=> Generate Bootstrap Loader (generally compiled by C)

=> Bootstrap Loader (generally written in java) loads Extended Loader and sets its parent to Bootstrap Loader.

=> Bootstrap Loader loads System Loader and sets the parent of System Loader to Extended Loader.

=> System Loader starts to load the specified class

Each loader will first hand over the loading class task to its parent. If the parent cannot be found, it will be loaded by itself. That is, Bootstrap => Extended Loader => System Loader => NoClassDefFoundError

Java class loader hierarchy

(1) Bootstrap Loader searches for the system parameter sun. boot. class. class at the specified position in path (the default value is in. class file or lib. jar file), you can use System. getProperty ("sun. boot. class. path.

(2) Extended Loader searches for system parameters in java. ext. files in the specified position in dirs (the default value is lib \ ext \ classes under the JRE directory. class file or. jar file), you can use System. getProperty ("java. ext. dirs.

(3) System Loader searches for the System parameter java. class. path. class file), you can use System. getProperty ("java. class. path.

(4) When the three loaders perform their respective duties, an exception or null will be thrown when the file is put in the wrong position.

6. When the loadClass () method is used to load a class, static blocks are not run.

7. Both ExtClassLoader and AppClassLoader are subclasses of java.net. URLClassLoader. After the program starts, both of them will have a copy in the virtual machine, and the path cannot be changed when the program runs. to dynamically load classes with other paths, a new loader is required.

Specifies the ExtClassLoader search path java-Djava. ext. dirs = c: \.

Specify the search path of AppClassLoader (set ClassPath) java-classpath c: \ path

 

Use URLClassLoader to generate a new loader.

URL url = new URL ("file:/c:/path ");

ClassLoader urlClassLoader = new URLClassLoader (new URL [] {url });

Class c = urlClassLoader. loadClass ("SomeClass ");

8. BootStrap Loader load ClassLoader

=> After a ClassLoader instance is added (its parent is automatically set to AppClassLoader), loadClass () is used to specify the class to be loaded.

9. Using the reflection mechanism, you can dynamically load classes and generate objects during runtime, operate methods on objects, change the value of class members, or even change the value of private variables.

10. newInstance () => instantiate the Object and return the Object type

Specify the parameter type and the target Constructor object. Use the newInstance () of the Constructor and specify the Acceptance value of the parameter => specify the initial value of the object when the object is dynamically loaded and generated.

11. added the java. lang. reflect. Proxy class after J2SE 1.3 to assist in implementing the dynamic Proxy function.

Implement Dynamic proxy function =>

Define the interface to be proxy =>

Use Proxy. newProxyInstance () to create a Proxy object =>

Call the InvocationHandler's invoke () method to pass in the method name and running variable of the proxy object, and hand over the actually running object to method. invoke ()

=> The object returned by method. invoke () is the return result after the actual method is run.

12. Ant build tool and Junit Test Tool

 

 

 

 

 

 

 

 

Related Article

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.