Comprehensive summary of JAVA basic knowledge [New students must see and learn together]

Comprehensive summary of java basic knowledge

1. What is the Java virtual machine? Why is Java called a "platform-independent programming language"? 2

2. What does the "static" keyword mean? Can a private or static method be overridden in Java? 3

3. Is it possible to access non-static variables in the static environment?

4. What are the data types supported by Java? What is automatic unpacking?

5. The difference between Overload and Override. Can the Overloaded method change the type of return value?

6. Does Java support multiple inheritance?

7. Basics: Final modifies basic data type variables and reference data type variables.

Advanced: Constants modified by final will be placed in the constant pool during compilation

Exploration: Why can only local/anonymous inner classes use external local variables when they are modified by final?

8. What is passing by value and passing by reference?

9. How many different ways are there to create threads? Which one do you like? why?

10. What is the difference between synchronization method and synchronization code block?

11. What is a deadlock (deadlock)?

Explore the role of final in java

① Variables modified by final cannot be changed. But "cannot be changed" here has different meanings for different data types. When final modified is a basic data type data, the value of this data It cannot be changed after initialization; when the final modification is a reference type data, that is, when modifying an object, the reference will always point to a memory address after initialization and cannot be modified. But the object information stored in the memory address, It can be modified.

②Advanced: The constants modified by final will be put into the constant pool during the compilation stage. Final is used to define constants. The advantage of defining constants is that there is no need to create the same variable repeatedly. The constant pool is a Java item An important technology, variables modified by final will be placed in the constant pool of the calling class during the compilation phase.

③Exploration: Why local/anonymous internal classes can only use final modified variables when using external local variables. Tip: After JDK1.8, when accessing external local variables through internal classes, there is no need to explicitly declare external local variables as final. It’s not that we don’t need to declare it as final, but the system does it for us during compilation. But we still need to understand why we use final to modify external local variables. So when using external local variables in internal classes You should use final to modify local variables, so that the value of local variable a will never change, which also avoids data inconsistency.

④ The final modification method uses the final modification method to have two functions. The first function is to lock the method and prevent any inherited classes from modifying it. The other function is to inline the method in the compiler to improve efficiency. But now there are very few In this way, the modern Java version has handled this part of the optimization very well.

⑤final modified class, the purpose of using final modified class is simple and clear: to indicate that this class cannot be inherited. When there is a class that will never be inherited in the program, you can use the final keyword to modify, all member methods of the final modified class Will be implicitly modified as a final method.

In-depth analysis of the keyword static in Java

① The basic concept of static keyword is convenient to call without creating an object.

In other words: modified by the static keyword does not need to create an object to call, and can be accessed directly based on the class name.

②The static keyword modifies the class, but there is a special use to modify the inner class with static. The ordinary class is not allowed to be declared as static, and only the inner class can be used.

③The static keyword modifies the method. When modifying the method, it is actually the same as the class, which can be called directly by the class name

④The static keyword modifies variables. The member variables modified by static are called static variables, also called class variables, indicating that this variable belongs to this class, not an object, and member variables that are not modified by static are called instance variables, indicating this variable It belongs to a specific object.

⑤The static keyword modifies the code block

The static code block is executed when the class is loaded for the first time. The main purpose here is to verify the order of class initialization.

[External link image transfer failed, the source site may have an anti-leech link mechanism, it is recommended to save the image and upload it directly (img-Mdc2vJoe-1592787631486)(https://pics3.baidu.com/feed/8644ebf81a4c510f633a3493cb792028d52aa567.jpeg?token=b0b04912bcd29617fe393&s393&s53 =E4906C32198A444D5C64ACDA0000C0B3)]

The following is a summary of the static keyword:

(1) Features:

1. Static is a modifier used to modify members. (Member variables, member functions) The member variables modified by static are called static variables or class variables.

2. Static modified members are shared by all objects.

3. Static takes precedence over the existence of objects, because static members already exist as the class is loaded.

4. The static modified member has one more calling method, which can be directly called by the class name (class name. static member).

5. The static modified data is shared data, and the storage in the object is unique data.

(2) The difference between member variables and static variables:

1. The difference in life cycle:

Member variables exist as objects are created and released as objects are recycled.

Static variables exist as the class is loaded and disappear as the class disappears.

2. Different calling methods:

Member variables can only be called by objects.

Static variables can be called by objects or by class names. (Recommended to call by class name)

3. Different aliases:

Member variables are also called instance variables.

Static variables are called class variables.

4. Different data storage locations:

Member variable data is stored in objects in heap memory, so it is also called object-specific data.

Static variable data is stored in the static area of ​​the method area (shared data area), so it is also called the shared data of the object.

(3) Matters needing attention in static use:

1. Static methods can only access static members. (Non-static can access both static and non-static)

2. This or super keywords cannot be used in static methods.

3. The main function is static

java memory structure diagram

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-aPtMCH2D-1592787631489)(https://pics3.baidu.com/feed/024f78f0f736afc33409f1471839eec1b74512b4.jpeg?token=33db0ecbe211c671b691794 =ED9CAA528ACE3EC846392E6303003066)]

From the above figure, we can find that static variables are stored in the method area and are shared by all threads. Here to talk about the java heap, the java heap stores the instance variables we create.

Heap area:

1. All objects stored are objects, and each object contains information about a corresponding class. (The purpose of class is to get operation instructions)

2. Jvm has only one heap area (heap) shared by all threads, basic types and object references are not stored in the heap, only the object itself

Stack area:

1. Each thread contains a stack area. Only basic data type objects and references to custom objects (not objects) are stored in the stack. The objects are stored in the heap area.

2. The data (primitive types and object references) in each stack are private and cannot be accessed by other stacks.

3. The stack is divided into 3 parts: basic type variable area, execution environment context, operation instruction area (store operation instructions). ,

Method area:

1. Also called static area, like heap, it is shared by all threads. The method area contains all class and static variables.

2. The method area contains elements that are always unique in the entire program, such as class and static variables.

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-pi1BBWML-1592787631491)(https://pics2.baidu.com/feed/f3d3572c11dfa9ec028d9199c8f0f206908fc147.jpeg?token=2a78319a30f2dfd3eebeae =2FA67D228BEF410B12F085D3000090B3)]

What is a deadlock (deadlock)

Deadlock: refers to the phenomenon of two or more processes waiting for each other due to competition for resources during the execution process. If there is no external force, they will not be able to advance
(1) because of insufficient system resources.
(2) The sequence of process operation is not suitable.
(3) Improper resource allocation, etc.
If the system resources are sufficient and the resource request of the process can be satisfied, the possibility of deadlock is very low, otherwise it
will fall into deadlock due to contention for limited resources. Secondly, the order and speed of process running advancement are different, and deadlock may also occur.
(1) Mutually exclusive conditions: A resource can only be used by one process at a time.
(2) Request and hold conditions: When a process is blocked by requesting resources, it keeps the acquired resources.
(3) Non-deprivation conditions: The resources already acquired by the process cannot be deprived forcibly before they are used up.
(4) Cyclic waiting conditions: A cyclic waiting resource relationship is formed between several processes.
These four conditions are the necessary conditions for deadlock. As long as the system is deadlocked, these conditions must be established, and as long as one of the above conditions is
not met, deadlock will not occur.
Removal and prevention
of deadlock : understanding the causes of deadlock, especially the four necessary conditions for deadlock, can avoid, prevent and
remove deadlock to the greatest extent possible . Therefore, in terms of system design, process scheduling, etc., pay attention to how to prevent these four necessary conditions from being established, and how to
determine a reasonable resource allocation algorithm to prevent processes from occupying system resources permanently. In addition, prevent the process
from occupying resources while in a waiting state . Therefore, reasonable planning should be given to the allocation of resources.

Guess you like

Origin blog.csdn.net/weixin_42462804/article/details/106895302