1.new object and Declaration object difference
People p = new people (); and poeple p;
The former creates the memory space above the heap and the P points to the space. The latter is just a null pointer and does not point to any storage address.
If the above P = man, then the former and the latter will be the same, but the first way to declare the object space is discarded, garbage collection at the time of the release of recycling.
2.static Cosmetic Class
For an inner class, to make it visible to other classes, and then call the subclass without first declaring the parent class. So you can call this inner class declaration static.
3.private, protected, and public can declare the constructor method, which is private for the general construction method of the single case pattern, and newinstance is the public static type, preventing the user from using the constructor method to construct the object directly.
4.Java currently has 9 corresponding JDK is 1.9, then jdk1.5,1.6,1.7,1.8, corresponding Java respectively is 5,6,7,8.
5.Java several garbage collector
Serial garbage Collector Parallel garbage Collector CMS garbage Collector G1 Garbage
Each of these four kinds of garbage collector has its own advantages and disadvantages. Most importantly, we developers can choose the type of garbage collector used by the Java Virtual machine. We can select them by passing the form of arguments. Each type of garbage collector has a very large feature that can provide completely different performance. This centralized garbage collector must be understood rigorously and accurately, and then chosen correctly based on the usage of the application. 1. Serial Garbage Collector
Serial garbage collector works by pausing all application threads. It is designed for a single threaded work environment. It uses a thread for garbage collection. This pause application thread for garbage collection may not be appropriate for the server environment. It is best for simple command-line programs.
The serial garbage Collector is selected by-XX:+USESERIALGC parameters. 2. Parallel Garbage Collector
Parallel garbage collector is also known as the throughput Collector (throughput collector). It is the default garbage collector for Java virtual machines. Unlike serial garbage collector, Parallel garbage uses multiple threads for garbage collection. When similar to serial garbage collector, it also suspends all application threads for garbage collection. 3. CMS Garbage Collector
Concurrent Mark Sweep (CMS) garbage collector uses multiple threads to scan heap memory to mark instances that need to be recycled, and then clear the flagged instances. CMS garbage collector suspends all application threads only in the following two scenarios: When you mark objects in a permanent memory space, there are some changes to the heap memory synchronization when garbage collection occurs.
Compared to the parallel garbage collector,cms garbage Collector uses more CPU resources to ensure that the application has a better throughput. If more CPU resources are allocated to achieve better performance, then CMS garbage Collector is a better choice than the parallel garbage Collector.
Using XX:+USEPARNEWGC parameters to select CMS garbage Collector. 4. G1 Garbage Collector
G1 garbage collector is used for large heap memory areas. It splits heap memory into separate zones (Region) and then garbage-collect them concurrently. After the memory is freed, G1 can also compress the idle heap memory. However, the CMS garbage collector is memory-compressed through "Stop the World". G1 first collects areas that can reclaim more memory.
The G1 garbage Collector is selected by –XX:+USEG1GC parameters.
Improvements in Java 8
When using G1 garbage collector, the-xx:+usestringdeduplication parameters can be opened. It optimizes the use of heap memory by moving repeated strings into the same char array. This option is referenced in Java 8u20.
A description of the four garbage collectors is given, which is determined by the application scenario, available hardware resources, and throughput requirements. garbage collection options in the Java Virtual machine
The following are Java Virtual machine options associated with Java collectors.
Garbage collector Selection
Option |
Description |
-xx:+useserialgc |
Serial Garbage Collector |
-xx:+useparallelgc |
Parallel Garbage Collector |
-xx:+useconcmarksweepgc |
CMS Garbage Collector |
-xx:parallelcmsthreads= |
Number of threads used by CMS collector– |
-xx:+useg1gc |
G1 Gargbage Collector |
Garbage Collection Optimization Options
Option |
Description |
-xms |
Heap Memory Initialization Dimensions |
-xmx |
Heap Memory Maximum size |
-xmn |
The size of the Cenozoic (young Generation) |
-xx:permsize |
Permanent generation (permanent Generation) initialization dimensions |
-xx:maxpermsize |
Permanent generation (permanent Generation) Maximum size |
Example of using Java Virtual machine garbage collection options
Java-xmx12m-xms3m-xmn1m-xx:permsize=20m-xx:maxpermsize=20m-xx:+useserialgc-jar Java-application.jar
For memory partitioning and management, it is generally the operating system (no matter what compiler compiles the program). But for Java virtual machines to manage their own memory heap, it is necessary to have their own partitions on the memory heap, and different versions of the JVM differ in memory partitioning.
6.Java Object Array
object[] Objarr = new object[1000];
string[] Strarry = new STRING[10]; String[] Strarry = {"," "," "," "," "," "};
four references in 7.java-strong references, soft references, weak references, and virtual references.
Strong reference: Regardless of whether the memory is sufficient, will not be recycled, even if the burst to Oom will not be recycled.
Soft reference: Reclaims the object associated with the reference when there is not enough memory.
Weak reference: Garbage collection, regardless of whether the memory is sufficient, will be recycled.
Virtual reference: can be reclaimed at any time by the garbage collector.
Strong references as long as there is a pointer to the heap, then the GC will not be recycled, even if thrown out oom. This pointer variable can be a local variable, where the method is executed, has been out of the stack, then the local pointer does not exist, then the GC can be recycled. But if a method creates many local objects, or an array of objects, the method is not yet out of the stack, then the GC, even oom, will not be recycled. Unless the pointer is empty after each execution of a local pointer. Run () {Object Obarr = new object[10000]; Object obarr1 = new object[10000]; ... }
Soft references (not enough memory to recycle), weak references (garbage collection is recycled) and virtual references (can be recycled at any time) in the recovery of the first GC will determine whether to use, there is no point to, point to certainly will not be recycled. Without a point or call, it is reclaimed. General use of these three references is to create larger storage space, such as pictures, file cache objects, or larger object arrays.
8.transient and Volatile keywords--thread safety and prevention of serialization
Transient
Transient is a type modifier and can only be used to modify fields. In the process of serializing an object, variables marked as transient are not serialized.
Example: Class Test {
transient int A; Will not be persisted
int b; Persistence of
}
When the instance object of class test is serialized (for example, the instance object T of the test class is written to the hard disk's text file t.txt), the contents of variable A are not saved, and the contents of the variable B are saved.
Reference:
The process of converting an object's representation into a byte stream is called serialization (also known as serializing, serialization), and the object is reconstructed from a stream of bytes called drag (also known as deserialization, deserialization). Transient provides a language-level method of marking data for data that should not be serialized.
Volatile
Volatile is also a variable modifier and can only be used to modify variables. Volatile decorated member variables are forced to reread the value of the member variable from shared memory each time it is accessed by the thread. Also, when a member variable changes, the thread is forced to write the change value back to the shared memory. So at any given moment, two different threads always see the same value for a member variable.
Explain the Java memory mechanism here:
Java uses one main memory to hold the variable's current value, while each thread has its own working memory. When a thread accesses a variable, it copies the value of the variable to its working memory, so that when the thread operates on a variable in its own working memory, the value of the copy of the variable in working memory is different from the value of the variable in the main memory. 9.Java Concurrent and concurrent containers--Concurrent copyonwritearraylist and Copy-on-write strategy
Copy-on-write abbreviation Cow, is a kind of optimization strategy used in programming. The basic idea is that from the beginning, everyone is sharing the same content, when someone wants to modify this content, it will really content copy out to form a new content and then change, this is a delay lazy strategy. The Java concurrency package, starting with JDK1.5, provides two concurrent containers implemented using the Copyonwrite mechanism, which are copyonwritearraylist and Copyonwritearrayset. The Copyonwrite container is useful and can be used in a very many concurrent scenarios.
The Copyonwrite container is the container that is copied when it is written. The popular understanding is that when we add elements to a container, we do not add them directly to the current container, but we copy the current container, copy the new container, add the element to the new container, add the element, and then point the original container's reference to the new container. The advantage of this is that we can read the Copyonwrite container concurrently, without requiring a lock, because the current container will not add any elements. So the Copyonwrite container is also a read-write separation of ideas, reading and writing different containers.
The Copyonwrite container can only guarantee the final consistency of the data and cannot guarantee the real-time consistency of the data. So if you want to write the data, you can read it right away, please do not use the Copyonwrite container.
Concurrentlinkedqueue
In concurrent programming, it is sometimes necessary to use thread-safe queues or lists. There are usually two ways to implement thread safety, one is to use a blocking algorithm, and one is to use a non-blocking algorithm. The implementation of Non-blocking algorithm is based on cyclic CAS (Compare and swipe comparison and exchange).
The Concurrentlinkedqueue technical implementation is similar to that of copyonwritearraylist and copy, but the container can be copied and modified with only a portion of the content rather than the entire container. The concurrentlinkedqueue consists of a head node and a tail node, each consisting of a node element (item) and a reference to the next node (next). Nodes are connected by next to form a queue of linked list structures. Concurrenthashmap and lock segment technology
Concurrenthashmap is a thread-safe and efficient hashmap. In a multithreaded environment, the use of HashMap can lead to a dead loop, as suggested in the article, Hashtable this obsolete container is inefficient (using synchronized to ensure thread safety). Concurrenthashmap use of the lock segmentation technology, greatly improve the efficiency of concurrent use.
Lock Segmentation Technology: Assuming that the container has multiple locks, each lock for the lock container part of the data, when multithreading access to different data segments of the container, there is no lock competition between the threads, thus increasing the efficiency of concurrent access.
10.public,protected,private permission modifiers, by default, are the default public in the same package, not within the same package private,protected only allow child classes to access. 11.Java rule: High position conversion requires strong transfer, high position conversion and automatic transformation. short a=1; This is right and can be directly assigned, as long as the value is within the short range. Short B =a +1; This is wrong, 1 the default is integral type. Short a +=1; this is right, Java Composite operations are optimized by adding short a+1 first and then converting the short type. The 12.Java switch can only receive data types in int, Short,byte,char four.