Java Programming Ideas-object-oriented and JVM basics

Source: Internet
Author: User
Tags multiple inheritance in c

4 Types of access permissions in 1.java

(1). Public maximum access control permission, which is visible to all classes.

(2). Protect the same package is visible, not all subclasses of the same package are visible.

(3). The default package access permission, that is, the classes in the same package can be visible, by default, do not explicitly specify access control permissions when it is the default package access rights.

(4). Private most strict Russian access control permission, only the class itself is visible, all classes are not accessible (the reflection mechanism can be accessed).

Access control permissions for non-internal classes can only be the default package access permissions or public, and cannot be protected and private. Access control permissions for internal classes can be protected and private.

Java only handles document comments for public and protected access control permission members, and document comments for private and default package access control permission members are ignored.

2. Object-oriented programming in two kinds of object combination methods--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.

A class with common properties and methods can abstract shared information into a parent class, enhancing code reusability and being the basis of polymorphism.

The extended part of the missing PIP class 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 refers to another class as its member variable in one class.

The advantages of scalability and flexibility are high. The has-a combination relationship should be given priority in the object composition relationship.

A derivation relationship is not seen between classes that have a common denominator.

3. polymorphic

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 is generally used in non-object-oriented programming languages, where the memory address of the 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 method to invoke the memory address of the body.

Advantages of single 4.java inheritance

Java only supports single inheritance of classes compared to multiple inheritance in C + +. The common base class for all classes in Java is the object class, the only root node of the object class Java class tree, which 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 conversion (upcast) and down type conversion (downcast)

(1). Type conversion up (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 are not visible, only the attributes that the subclass inherits from the parent are still visible, and the compiler automatically checks if type compatibility is 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 to store data in 7.java

(1). Register (Registers)

Located inside the CPU, is the fastest storage area, but the number and capacity is limited. Registers cannot be manipulated directly in Java.

(2). Stacks (Stack)

The stack is located in Universal 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 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. But the disadvantage is that the data size in the stack and the lifetime must be deterministic and inflexible.

(3). Heaps (heap)

is also a shared memory pool located in Universal 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 the runtime dynamically allocated memory of the Java garbage collector will automatically take away these no longer used data. However, the disadvantage is that the memory is allocated dynamically at runtime and the access speed is slow.

(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, typically stored outside of the program, such as a hard disk.

Assignment operation in 8.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, does not allow the argument to be affected by a workaround, first clones the reference in a method, and then operates on the cloned object.

9. Shift Operations

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 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 of the first 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-hand side removes all 0.

in 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.

11. Method overloading The (overloading) method has the same name, the parameter list is called a method overload, and the return value type of the method cannot be used as a method overload.

Destructors in 12.java

There is no destructor in Java, such as C/T + +, to destroy unused objects for memory space, only the following three methods are used to notify the garbage collector to reclaim objects.

(1). Finalize () Just 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. The most useful part of Finalize () is to call the destructor of the local method to consume the object release function when JNI calls the local method (C/S method).

(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, and the garbage collector decides whether to recycle based on the current memory usage and the life cycle of the object.

(3). Runtime.getruntime (). GC () and System.GC () are similar.

Note that these three functions do not guarantee that the garbage collector executes immediately and is not recommended for frequent use.

13. Garbage collector principle

(1). Reference count (referencecounting) garbage collection algorithm

A simple but slow garbage collection algorithm, each object has a reference counter (Reference Counter), and when each reference is attached to the 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. Garbage collection, which traverses the entire list of objects, moves the object out of memory release when it finds that the reference counter for an object is 0 o'clock.

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 reference counting algorithm is only used to explain how the garbage collector works  no JVM uses it to implement the garbage collector. An improved algorithm for reference counting any surviving object must be referenced by a reference in a static store or stack (stack), so all surviving objects can be determined when traversing a reference in all static stores or stacks. Whenever a reference is traversed, the object pointed to by the reference is checked and all references on that object are not referenced by the object and objects that are referenced to each other are reclaimed by the garbage collector.

(2). Pause Replication (stop-and-copy) algorithm

The collection mechanism of the garbage collector is based on any surviving object that must be referenced by a reference stored in a stack or static store. The algorithm for pausing replication is that the program pauses execution first while it is running, copying every surviving object from one heap to another that is no longer in use and is not being copied.

There are two problems with pausing the replication algorithm:

A. It is necessary to maintain a separate two heap that requires twice times the memory space required for the program to run. The workaround for the JVM is to simply copy from one block of memory to another when allocating heap space replication in a memory block.

B. The second problem is that the replication process itself handles only a few garbage objects that need to be recycled when the program is running stably. If the garbage collector still replicates the surviving objects frequently, it is 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-clearing algorithm is slow in normal usage scenarios, but the algorithm is very efficient when the program only produces very few garbage objects that need to be recycled.

(3). Mark Clear (Mark-and-sweep) algorithm

And the logical-like markup cleanup algorithm that suspends replication starts tracing all references from the stack and static storage to find the surviving object. Each time a surviving object is found, the object is set to a tag and is not recycled. When the tagging process is complete, clear unused dead objects to free up memory space. The tag cleanup algorithm does not need to replicate all of the markup and cleanup work done in a 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.

(4). Generational replication (generation-copy) algorithm

An improvement to the pause replication algorithm, where the JVM allocates memory is allocated in blocks, and when a large object is created, it takes up a block of memory space, and a strict pause-copy algorithm requires that each surviving object be copied from the source heap to the new heap before releasing the old memory heap, which consumes memory very much. The memory heap garbage collector allows you to copy objects into the memory heap of reclaimed objects, each with a generation count (generation count) used to mark whether an object survives. Each block of memory is referenced by the object to obtain a generation count, and a new block of memory is typically created only when the oldest memory block is reclaimed, which is primarily used to handle a large number of short-lived temporary object reclamation issues. During a complete cleanup process, large objects in the block of memory are not copied, but the generation count is re-obtained based on the reference.

The JVM monitors the efficiency of the garbage collector:

When all objects are found to survive for a long time, the JVM adjusts the garbage collector's collection algorithm to mark cleanup, and when the memory heap becomes fragmented, the JVM re-switches the garbage collector's algorithm to pause replication. This is the idea that the JVM's adaptive generational pause replication flag clears the garbage collection algorithm.

14.java Instant Compilation Technology (JIT)

JIT technology is the Java code is partially or completely converted to the cost of the machine code program, no longer need the JVM interpretation, execution faster. When a ". Class" class file is found, the byte code of the class file is called into memory, and the JIT compiler compiles the bytecode code.

JIT has two disadvantages:

(1). JIT-compiled conversions take some time, which runs through the entire life cycle of the program.

(2). The JIT increases the size of the executable code, and the JIT code expands the size of the code compared to the compressed bytecode, which can cause memory paging and thus slow program execution. One of the improved techniques for JIT insufficiency is the deferred evaluation (lazy evaluation), whose basic principle is that bytecode is not JIT-compiled immediately unless necessary, a lazy JIT-like hotspot method is used in the nearest JDK to optimize each code execution. The more code executes, the faster it gets.

High-precision numeric types in 15.Java

BigInteger and BigDecimal are high-precision numeric types in Java, and because they are the basic data types used to wrap Java, there is no corresponding primitive type for these two high-precision numeric types.

(1). BigInteger supports integers of arbitrary precision, that is, using BigInteger to represent an integer value of any length without losing information in the operation because of a range overflow.

(2). The bigdecimal supports arbitrary precision fixed-number floating-point numbers, which can be used to accurately calculate values such as currency. The normal float and double floating-point numbers are limited by the number of decimal places, they can not be accurately compared in the operation, only the error range is determined to be equal, and the BigDecimal is able to support the fixed number of floating-point numbers and perform accurate calculation.

Assignment operation in 16.Java

(1). Primitive type Assignment operation

8 Primitive data type assignment operations. Values on the other side of the assignment operator will not be affected by the value on the other side of the assignment operator after the primitive type assignment operation.

(2). Assignment operation of Reference type

All data types except the original data type in 8 are object types, and the assignment of an object type is an action reference, such as A=B, which assigns a B reference to a reference, where the object referred to by the B reference is now pointed to by A and B references. The reference assignment operator is also called the alias operator, which is the equivalent of an alias to the reference object, which is actually referred to the same object. An assignment operation of a reference type that, if a reference on either side of the assignment operator alters the value of the referenced object, is also affected by the reference on the other side of the assignment operator.

Java Programming Ideas-object-oriented and JVM basics

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.