"Java Programming thought" study notes (i)

Source: Internet
Author: User

the object-oriented and JVM Basics
4 Types of access permissions in 1.java:
(1). public: Maximum access control permissions, which are visible to all classes.

(2).Protect: The same package is visible, not all subclasses of the same package are visible.
(3).default: Package access, that is, the classes in the same package can be visible. The default package access control permission is not explicitly specified when Access control permissions are not explicit.
(4).Private: The most stringent Russian access control permissions, only the class itself is visible, all classes are not accessible (the reflection mechanism can be accessed).
2. Object-oriented programming in two combinations of objects--is-a and has-a:
(1).is-a Combination: One class inherits another class that has similar functionality, and expands on the basis of the inherited class as needed.
Pros: Classes with common properties and methods can abstract shared information into the parent class, enhancing code reusability and being the basis of polymorphism.
Disadvantage: The extended part of the subclass is not visible to the parent class, and the use of inheritance increases the redundant code if there is less commonality.
(2).has-a Combination: The has-a combination is referencing another class as its member variable in one class. Advantages: High scalability and flexibility. The has-a combination relationship should be given priority in the object composition relationship. Disadvantage: A derivation relationship is not visible between classes that have commonalities.
3. Polymorphism:
In object-oriented programming, a method in which a subclass has the same method of signing as a parent class is called a subclass method that overrides the parent class method, and when an operation of a subclass method is called, it is not necessary to know exactly what the subclass is, but to call the subclass type as a reference to the parent class, at run time, The JVM calls the method that should be based on the specific subclass type of the Reference object, which is polymorphic.
The basis of polymorphism is the late binding mechanism of Java object-oriented programming. There are two types of binding mechanisms in programming:

(1). early binding : typically used in non-object-oriented programming languages, the memory address of a specific calling method body is computed at the time of program compilation.

(2). Late binding : Object-oriented programming language is often used in the program at compile time can not calculate the specific call method body memory address, only the method parameter type and return value type of the checksum, at run time to determine the specific to call the method body memory address.

Advantages of single 4.java inheritance:
Compared to C + + inheritance, Java only supports single inheritance of classes, the common base class for all classes in Java is the object class, the object class Java class tree Unique root node, this single inheritance has the following benefits:

(1). Single inheritance ensures that all objects have some common characteristics, which makes it convenient for the JVM virtual machine to perform system-level operations on all classes, and all Java objects can be easily created in the memory stack, and passing parameters is easier and easier.
(2). Single inheritance in Java makes it easier to implement the garbage collector because it ensures that the JVM knows the type information for all objects.


5. Select a Container object two principles:
(1). The container can provide different types of interfaces and external behavior to meet the requirements.

(2). Different containers are different for different operating efficiency.


6. Type conversion:
There are two common types of conversions in Java: Up-type conversions (upcast) and down-type conversions (downcast):
(1). Upward type conversion (upcast):
The up-type conversion is to cast a subclass object into a parent class type, and classic usage is an object-oriented polymorphic attribute. When the type is converted, the attributes of the subclass object will not be visible, and only the attributes inherited by the subclass from the parent class remain visible, and the compiler automatically checks for type-compatible, usually safe, when the type is converted.
(2). Down type conversion:
The down-type conversion is to cast the parent class type to the subclass type, the non-visible subclass attribute in the parent class reverts to the visibility, and the compiler cannot automatically detect if the type is compatible, often resulting in a run-time exception of type conversion errors, usually unsafe.

5 places in 7.java where data is stored:
(1). Register (Registers): located inside the CPU, is the fastest storage area, but limited in quantity and capacity. Registers cannot be manipulated directly in Java.
(2). Stack : The stack is located in Universal Random access memory (general random-access Memory,ram, memory), accessed through the processor's stack pointer, and the stack pointer allocates memory from the top of the stack to the bottom of the stack, freeing the memory from the bottom of the stack. The stack is the second fastest memory behind the register, in Java programs, the general reference to 8 basic types of data and objects is usually stored in the stack memory, and the string object that does not pass the New keyword is also stored in the string pool of the stack. The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible.
(3). heap: A pool of shared memory that is also located in Universal Random access memory (general random-access Memory,ram, memory). The Java heap is a runtime data area where objects from classes are allocated space, and objects created with the new keyword are stored in heap memory, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.
(4). constant Memory (Constant storage):Constants in Java are stored in read-only memory embedded in the system (Read-only memory,rom).
(5). non-random memory (Non-ram storage): for Stream objects and persisted objects, usually stored outside of the program, such as a hard disk.


8.javadoc only handles document comments for public and protected access control permissions, and stable annotations for private and default permissions are ignored.


Assignment operation in 9.java:
The base type assignment is a direct copy of the value, which does not affect each other after the assignment operation.
A reference type assignment is a copy of a reference value, which is equivalent to an alias to an object, after which two references point to the same reference object and have an effect on each other.
In Java, passing a reference type parameter to a method alters the value of the parameter and does not allow the argument to be affected by the workaround: First clone the reference in a method and then manipulate the cloned object.


10. Shift Operation:
The left shift operator <<: Shifts the bit left by the specified number of digits, the right part is 0, and the left one is equal to 2.

Right-shift operator >>: Shift the bit to the right of the specified number of digits, if it is positive, the left first bit (sign bit) 0, the remaining bit 0, if it is negative, the left first bit 1, the remaining bits complement 0. Move right one equals except 2.

the unsigned right-shift operator >>>: Shifts the bit right by the specified number of digits, either positive or negative, and the left side removes all 0.


In 11.java, when a mathematical or bitwise operation is performed on a primitive type (char, byte, short) that is smaller than the int type, the data type is first converted to the int type and then the corresponding operation is performed.


12. Method overloading (Overloading): Method with the same name, different parameter list is called method overload, note that the return value type of the method cannot be used as a method overload.

13.java:

(1). only notifies the JVM that the current object of the garbage collector is no longer used can be recycled, but the garbage collector determines whether to recycle based on memory usage.
finalize () is most useful when the local method is called by JNI (c/S method), the destructor of the local method is consumed by the object deallocation function.
(2). system.gc () is a forced destructor that explicitly notifies the garbage collector to free memory, but the garbage collector does not necessarily do so immediately. The garbage collector decides whether to recycle based on the current memory usage and the life cycle of the object.
(3).
Note: None of these three functions guarantees that the garbage collector executes immediately and is not recommended for frequent use.




( Reference Counter), when each reference is attached to this object, the object's reference counter is incremented by 1. When each reference goes out of scope or is set to NULL, the object's reference counter is reduced by 1. The garbage collector traverses the entire list of objects, and when a reference counter of an object is found to be 0 o'clock, the object is moved out of memory release. The disadvantage of the
reference counting algorithm is that when an object is looped to each other, the object's reference counter is not always 0, and additional processing is required to reclaim those objects. The


Any surviving object must be in a static store or stack (stack) Reference, so when you traverse a reference in a full static store or stack, you can determine all the surviving objects. Whenever a reference is traversed, the object pointed to by the reference is checked, and all references on that object are checked, the objects that are not referenced and the objects that are self-referencing are reclaimed by the garbage collector.

(2). Pause Replication (stop-and-copy) algorithm:
The garbage collector's collection mechanism is based on: Any surviving object must be referenced by a reference stored in a stack or a static store.
The algorithm for suspending replication is that the program pauses execution first during the run, copying each surviving object from one heap to another, and the objects that are no longer being used are recycled and no longer replicated. There are two problems with pausing the replication algorithm:
A. It is necessary to maintain a separate two heap at the same time, requiring twice times more memory space for the program to run. The solution to the JVM is to allocate heap space in a memory block, which is simply copied from one block of memory to another.
B. The second problem is that the replication process itself is handled, and when the program runs stably, it only produces very few garbage objects that need to be recycled, and if the garbage collector replicates frequently the surviving objects are very low performance.

the workaround for the JVM is to use a new garbage collection algorithm--tag Cleanup (mark-and-sweep). generally speaking, the tag cleanup algorithm is slower in normal usage scenarios, but it is very efficient when the program only produces very few garbage objects that need to be recycled.

(3). Mark Clear (mark-and-sweep) algorithm:
Similar to the logic for pausing replication, the tag cleanup algorithm tracks all references from stacks and static stores to find surviving objects, and when a surviving object is found, the object is set to a token and is not recycled, and when the marking process is complete, the unused dead object is cleared, freeing the memory space.
The tag cleanup algorithm does not need to replicate objects, and all markup and cleanup work is done in one memory heap.

Note: Sun's documentation says that the JVM's garbage collector is a low-priority process running in the background, but not implemented in earlier versions of the JVM, and when memory is insufficient, the garbage collector pauses the program and then garbage collection.

"Java Programming thought" study notes (i)

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.