Java commonly used keywords understanding

Java commonly used keywords understanding

There are many keywords in Java, about 50+. We cannot conflict with these keywords in terms of naming. Compilation will report errors. Each keyword represents a different meaning in different scenarios. Next, we choose a few more important Of keywords, learn more.

One: static

Static means static and global. Once it is modified, it means that the modified thing is shared within a certain scope and anyone can access it. At this time, you need to pay attention to the issue of concurrent reading and writing.

1.1, modified objects

static can only modify class variables, methods and method blocks.

1), modified class variables

When static modifies a class variable, if the variable is public, it means that the variable can be directly accessed by any class, and there is no need to initialize the class, just use the class name.static variable to access it directly.

At this time we need to pay attention to the issue of thread safety, because when multiple threads read and write shared variables at the same time, concurrency problems are likely to occur, so if you want to avoid this problem, it is best to visit each time Manually lock

2), modification method

When static modifies a method, it means that the method has nothing to do with the current class, and any class can be accessed directly (if the permission is public).

One thing to note is that this method can only call methods that are also modified by static, and cannot call ordinary methods. Various methods in our commonly used util classes are all decorated with static.

The variables inside the static method are not thread-safe during execution. When the method is executed, the data runs in the stack, and each thread of the stack data is isolated, so there will be no thread safety issues. Therefore, we can use the static methods of the util class with confidence.

3) Modification method block

When static modifies a method block, we call it a static block. The static block is often used to initialize some values ​​before the class starts, such as:

private static String ROOT_URL;

static {
    
    
    if (Constants.debug) {
    
    
        ROOT_URL = "调试环境下的URL";
    } else {
    
    
        ROOT_URL = "正式环境下的URL";
    }
}

This code demonstrates that the static block does some initialization work, but it should be noted that the static block can only call variables that are also modified by static, and the static variables need to be written in front of the static block, otherwise the compilation will report an error.

1.2 Timing of initialization

The method modified by static will not be initialized when the class is initialized. It will be executed only when it is called, and static variables and static blocks are initialized prior to the class constructor.

Two: final

Final means unchanged, and is generally used in the following three scenarios:

  1. A class modified by final indicates that the class cannot be inherited;
  2. The final modified method indicates that the method cannot be overwritten;
  3. A variable modified by final means that the variable must be initialized when it is declared, and its memory address cannot be modified later;

Note : The third point, we said that the memory address cannot be modified, and did not say that the value cannot be modified. For details, see the immutability of String .

Three: volatile

Volatile means visible. It is often used to modify a shared variable. It means that when the value of a shared variable is modified, it will be notified to other threads in time, and other threads can know that the value of the current shared variable has been modified.

In a multi-core CPU, in order to improve efficiency, the thread directly deals with the CPU cache instead of the memory when getting the value. The main reason is that the CPU cache execution speed is faster. For example, if the thread wants to get the value C, it will get it directly from the CPU cache. If there is no CPU cache, it will get it from the memory, so the thread read operation always takes the value of the CPU cache. .

At this time, there will be a problem. The value in the CPU cache and the value in the memory may not be synchronized at all times. As a result, the value calculated by the thread may not be the latest. The value of the shared variable may have been modified by other threads. The time modification is the value of the machine memory, and the value of the CPU cache is still old, causing calculation problems.

At this time, there is a mechanism, that is, the memory will actively notify the CPU cache. The value of the current shared variable has expired, you need to pull a copy again, and the CPU cache will fetch the latest value from the memory again.

The volatile keyword will trigger this mechanism, and the variable with the volatile keyword will be recognized as a shared variable. After the value in the memory is modified, each CPU cache will be notified, so that the value in the CPU cache will be modified accordingly , So as to ensure that the value that the thread fetched from the CPU cache is the latest.

Four: default

The default keyword is generally used on interface methods, which means that for this interface, subclasses do not need to implement mandatory, but they must have a default implementation.

Five: interview questions

5.1 How to prove that static static variables have nothing to do with classes?

  1. We can directly use static variables without initializing the class;
  2. We write a main method in the class to run, even if we do not write the code to initialize the class, the static variables will be initialized automatically;
  3. Static variables will only be initialized once. After the initialization is completed, no matter how many new classes I come out, static variables will not be initialized again;

Not only static variables, static method blocks are also irrelevant to classes.

Six: Summary

Java keywords are relatively basic content, and we need to be clear about their meanings to understand why such keywords are used here. For example, how the String source code uses the final keyword to achieve immutability, such as how the Map in the Java 8 collection uses the default keyword to add various methods.

Guess you like

Origin blog.csdn.net/weixin_38478780/article/details/107779887