Java Study Notes
Java Basic
The base type is not new, it is placed in the stack, the object's reference is also placed inside the stack, as long as the new () to create the object, will be created in the heap
The string class is designed to be immutable (immutable) classes. If you want to change its value, yes, but the JVM silently creates a new object at run time based on the new value, and then returns the address of the object to the reference of the original class. This creation process is entirely automatic, but it takes up more time. In the environment that is more sensitive to time requirements, it will have some adverse effects.
About the internal work of string str = "abc". Inside Java, this statement is translated into the following steps:
(1) First define an object reference variable named str to the String class: String str;
(2) in the stack to find whether there is a value of "ABC" address, if not, then open a store with a literal "ABC" address, then create a new String Class object O, and the string value of O point to the address, and in the stack next to this address note the referenced object o. If you already have an address with a value of "ABC", look for the object o and return the address of O.
(3) Point Str to the address of the object o.
It is important to note that the string values in the generic string class are directly stored values. But like string str = "abc"; In this case, the string value is a reference to the data in the existing stack!
Two conditions for Java memory leaks: useless, not recyclable.
BigInteger can represent arbitrary precision integer, BigDecimal can represent arbitrary precision floating-point number, but all is the method of precision speed change
Container Collection
List of sub-class inside LinkedList and ArrayList is non-synchronous, and vector and stack is synchronous, linkedlist the bottom of the two-way linked list (the front pointer + data + after the pointer), so the query efficiency is low, and the efficiency is high; ArrayList bottom is an array, so the query efficiency is high, and the efficiency is low; VECOTR is a heavyweight list that is typically used when concurrency guarantees thread safety
If you want to track the order of the elements that are added to HashSet, the Linkedhashset implementation can help. Linkedhashset iterators Access each element in the order in which they are inserted.
The elements in the list are sequential, repeatable, and the elements in the set are unordered and non-repeatable (SortedSet is ordered)
The next method of iterator jumps to the next element and returns the element that was just skipped, so when you use the Remove method, you must first call the next method
HashMap is the best choice for inserting, deleting, and locating elements in a map. But if you want to traverse the key in natural order or in a custom order, TreeMap is better.
Linkedhashmap expands hashmap to add keyword/value pairs into the link hash map image in order of insertion.
Hashtable is similar to HASHMAP, but does not allow null keys and null values. It is also slower than HASHMAP, because it is synchronous.
Efficiency
When variables are accessed frequently within a method, using local variables is more efficient than using instance variables and static variables.
Exceptions are bad for performance. Throwing an exception begins with creating a new object. The constructor of the Throwable interface calls the local (Native) method named Fillinstacktrace (), the Fillinstacktrace () method examines the stack, and collects the call trace information. Whenever an exception is thrown, the VM must adjust the call stack because a new object is created during processing.
I/O
Any Java IO object capable of generating a data stream (source) can be thought of as a InputStream object, and a Java IO object capable of receiving a data source (stream) can be seen as a OutputStream object
IO Data Flow There are two categories, one is Inputstream/outputstream, one is Reader/writer, respectively, to deal with byte stream and character stream, characters are also a kind of bytes, why use Reader/writer it? This is because the processing of characters may involve transcoding, the transcoding is mainly between reader and InputStream, and writer and OutputStream, The transcoding process is done through InputStreamReader and outputstreamwriter, and sometimes we may not explicitly invoke these two classes, but the related classes do this internally. For example, FileReader is actually through the InputStreamReader data from the FileInputStream, such as the operating system to convert the text encoding to Unicode (because in Java is the use of Unicode to save data), Remember that there is a premise that the current encoding format of the operating system and the encoding format of the document are always the same, if inconsistent, you must specify the encoding format of the current InputStream. If you don't know what encoding to use for a character, Instead of using reader and writer, you should instead use InputStream and OutStream
Buffering of input/output streams can improve code execution efficiency by encapsulating legacy streams using BUFFEREDXXXX and bufferedxxxx, which is a very important and basic technique for accelerating I/O access
The final keyword-Modified variable indicates that the variable is immutable, but the invariant here is only the reference itself, and there is no constraint as to whether the referenced object changes, such as the final StringBuffer a=newstringbuffer ("immutable"); A.append ("broken!"); Compiled, we can see that final is only valid for the reference "value" (that is, the memory address of the object it points to), forcing the reference to point only to the object that was initially pointed to, changing its point to cause a compiler error, and final is not responsible for the change in the object it points to.
When you want to clone a class containing a modifiable reference field, then you must be the entire class of the blueprint to copy, if you clone the object to modify the time will also affect the original instance, then this is not advisable. So it should be. Clone ()
JVM
In general, we need to inherit the abstract class Java.lang.ClassLoader when we use the virtual machine's class load, the method that must be implemented is loadclass (), for this method needs to do the following: (1) Confirm the name of the class; (2) Checks whether the requested class is loaded, (3) checks whether the requested class is a system class, (4) attempts to obtain the requested class from the store of the class loader, (5) defines the requested class in the virtual machine, (6) resolves the requested class, and (7) returns the requested class.
The loading process of classes in Java is also the process of agent loading. For example, a JVM in a Web browser needs to load a small application testapplet. The JVM invokes a small application loader ACL (applet ClassLoader) to complete the mount. The ACL first requests its parent loader, i.e. whether the system loader mounts the Testapplet to load this class, because the Testapplet is not in the load path of the system loader, so the system loader does not find this class, and there is no load success. Then the ACL itself loads the testapplet. The ACL successfully found the Testapplet.class file through the network and imported it into the JVM. During the load process, the JVM discovers that Testappet is inherited from the superclass Java.applet.Applet. So the JVM calls the ACL again to load the Java.applet.Applet class. The ACL again loads the applet class in the order above, and the ACL finds that his parent loader has loaded the class, so the ACL directly returns the loaded class to the JVM, completing the loading of the applet class. Next, the superclass of the applet class is also treated as such. Finally, Testapplet and all related classes are loaded into the JVM.
When the JVM needs to use a class, it requests the class according to its name to ClassLoader, and then ClassLoader attempts to return a class object that represents it. You can create custom ClassLoader by overriding the methods that correspond to the different stages of the process. There is a loadclass (String name, Boolean resolve) method, which is the entry point for ClassLoader, After jdk1.2, the LoadClass method will call the Findclass method by default, the detailed content can refer to the API documentation, we write the ClassLoader mainly to cover the above two methods.
java EE
The process by which Tomcat server processes an HTTP request
Suppose the request from the customer is:
http://localhost:8080/wsota/wsota_index.jsp
1) The request is sent to the native port 8080, where the Coyote http/1.1 connector that is listening is obtained
2) connector the request to the engine of the service it is working on and waits for a response from the engine
3) The engine obtains the request localhost/wsota/wsota_index.jsp, matches all the virtual host hosts it owns
4) The engine matches the host named localhost (even if it does not match the request to the host processing, because the host is defined as the engine's default host)
5) localhost host gets the request/wsota/wsota_index.jsp, matching all the context it owns
6) host matches to the context where the path is/wsota (if the match is not matched, the request is given to the context of the path named "" to process)
7) path= "/wsota" The context gets the request/wsota_index.jsp, in its mapping table to find the corresponding servlet
8) context matches to a servlet with a URL pattern of *.jsp, corresponding to the Jspservlet class
9) constructs the HttpServletRequest object and the HttpServletResponse object, calling Jspservlet's Doget or Dopost method as a parameter
) context returns the HttpServletResponse object to host after it has finished executing
) host returns the HttpServletResponse object to the engine
) engine returns the HttpServletResponse object to connector
Connector return the HttpServletResponse object to the customer browser
JDK new Features
Note (Annotation), you must first mention what is metadata (metadata). The so-called meta-data is data data. In other words, metadata is descriptive data. Just like the fields in a data table, each field describes what the data under this field means. The annotations provided in j2se5.0 are the metadata of the Java source code, which means that the comments are descriptive of the Java source code.
MultithreadingThreads
There are two ways to implement threading, one is to inherit the thread class and one to implement the Runnable interface. The method of implementing the Runnable interface is preferred. First, put the data that needs to be shared in a class that implements the Runnable interface, and then pass the instance of this class to the constructor of multiple thread. In this way, the newly created multiple thread, together with a runnable instance, shares the same data. If you take a method that inherits the thread class, you will have to use static statically members. If you share more data, you need a large number of static members, which makes the program data structure confusing and difficult to expand. This situation should be avoided as much as possible. Another benefit of using runnable is that multithreaded application objects can inherit other objects rather than having to inherit the thread class, thereby increasing the flexibility of multithreaded objects.
The process of multithreaded execution is when a program (a thread) executes, by calling Thread.Start () and letting the current program (thread) Continue executing, the thread of the start executes the code in the Run () method at the same time. The process of executing multiple pieces of executable code alternately.
Because multiple threads have a strong execution of PvP and program execution contexts, there is no problem of resource sharing in defining local variables in thread methods, and resource sharing synchronization occurs primarily on member variables.
Depending on the state of the line Cheng in the computer system's memory, threads can be divided into types such as create, ready, run, sleep, suspend, and die. These thread state types are characterized by:
Create state: When a Thread object instance is created with the new keyword, it exists only as an instance of an object, and the JVM does not run resources for threads such as CPU time slices;
Ready state: The Start method is called in the created state to convert the state of the thread to the ready state. At this point, the thread has been given other system resources except CPU time, so that the thread scheduler of the JVM schedules the thread according to the priority of the thread, so that the thread has the opportunity to get the CPU time slice.
Sleep state: During a thread run, you can call the Sleep method and, in the method parameter, specify the sleep time of the threads to convert the thread state to sleep. At this point, the thread stops running the specified sleep time without releasing the resource. After the time arrives, the thread is re-dispatched and managed by the JVM thread scheduler.
Pending state: You can convert the state of a thread to a pending state by calling the Suspend method. At this point, the thread frees up all the resources it consumes, and the JVM dispatches it to the temporary storage space until the application calls the Resume method to resume the thread to run.
Death state: When the thread body runs the end or the thread object's Stop method is called, the thread terminates, and the resource consumed by the thread is retracted by the JVM.
A special thread in Java is a low-level thread called a daemon (Daemon) thread. This thread has the lowest priority for serving other objects and threads in the system. The way to set a user thread as a daemon is to invoke the Setdaemon method of the thread object before it is created. A typical example of a daemon thread is a system resource recycling thread in the JVM that is always running in a low-level state for real-time monitoring and management of recyclable resources in the system.
The JVM fixed the relationship between thread and memory, the JVM's memory is divided into main memory and working memory, the data that the thread wants to manipulate is put in working memory, the variable of working memory is the copy of main memory. Variables in the working memory after the thread executes will also need to update the corresponding variables in the main memory. For code that uses synchornized protection, the main memory variable is locked to ensure that the variable between the main memory and the working memory is consistent between the newspapers. However, the performance of the code using synchonrized can be very costly
Threadlocal is not a local implementation version of a thread, it is not a threaded, but a threadlocal variable (thread local variable). Perhaps it would be more appropriate to name it Threadlocalvar. The thread local variable (ThreadLocal) function is very simple, that is, each thread that uses the variable provides a copy of the value of a variable, and each thread can independently change its own copy without conflicting with the other thread's copy. Its interior can actually be understood as only a entry map, key is the current thread, and value is the variable to save. So it only has two methods set () associates the variable with the thread, the Get () method takes the variable from the map, Another is the InitValue method, used to generate its own threadlocal, to give the default value, called in the Get and set methods
Java Basics-02