preface

This is recommended to learn Java series of the third, this section of the content of a lot, for xiaobai, are new concepts, to master, need a lot of practice. In addition, this section will involve a lot of system apis, and we need to learn how to read the JDK documentation, so we will also provide you with jdK1.8 help documentation, download at the end of the article.

Figure has the truth not to say much, the knowledge point is basically two times that on one.

Design patterns

Java 23 design patterns in the development of a lot of applications, not only our usual application development, but also some framework and system source code are applied, the specific content can not be mechanically applied, but in experience to experience the process, thinking about the application of design patterns and scenarios.

The singleton design pattern is the most common and the simplest. It can be said that almost every program developed with Java language will use the singleton design pattern. There is no need to get stuck on the concept, the important thing is to understand the idea, as the accumulation of development experience, you will slowly understand the mystery!

/** ** */
public class SingletonLazy {

    private static SingletonLazy singletonHungry;

    private SingletonLazy(a) {}public static SingletonLazy getInstance(a) {
        if (singletonHungry == null) {
            singletonHungry = new SingletonLazy();
        }

        returnsingletonHungry; }}Copy the code
/** ** hungry */
public class SingletonHungry {

    private static SingletonHungry singletonHungry = new SingletonHungry();

    private SingletonHungry(a) {}public static SingletonHungry getInstance(a) {
        returnsingletonHungry; }}Copy the code

polymorphism

Polymorphism is one of the three major features of Java object orientation, and I covered encapsulation and inheritance in the previous article. Polymorphism is the first focus of this section. In a nutshell: the multiple manifestations of a transaction are called polymorphisms.

It’s hard to understand in the abstract, but it’s easier to understand the actual scenario:

For example, we software developers themselves are abstract, because it is not easy for people in other industries to understand, and it is not clear, what are you developing? (Front-end or back-end development? Java development or PHP development? System-level development or game development? , etc.), the overall give a person is a kind of abstract concepts, but also probably know the word which has generality, which is abstracted from the generality of the ability of concrete in Java called abstract method, because each concrete developers have the same method of internal performance is different, this is the concrete subclass to achieve. Subclasses here can be understood as Java development engineer, PHP development engineer, and so on.

In our actual development, according to the business scenario, we have to extract the abstract class, and then realize the ability of separation, and the separation of a number of subclasses, with different forms of expression, is a variety of forms, so the name polymorphisms.

The keyword instanceof

As you can see, we’ve isolated the abstract classes (superclasses), so how do subclasses behave differently, and how does a superclass tell if a subclass is a subclass? This is where the keyword instanceof comes in.

One of the most common phrases you’ll probably hear in learning about polymorphism is that a parent class reference refers to a subclass object.

A reference is a variable of an object that is created. For example:

// programmer is a handle, also called a reference
Programmer programmer = new Programmer();
Copy the code

Here’s another example of a parent class reference to a subclass object:

// New is used to create objects, as we did in the previous section
Programmer javaProgrammer = new JavaProgrammer();
Copy the code

interface

Look at the diagram and remember how to use it and why. In real development, there are so many scenarios for defining interfaces that you must be familiar with the usage rules.

summary

The content of polymorphism is the most important, we must understand the following concepts and why? This is also a common interview question.

  1. How do you understand polymorphism in Java?
  2. What do you think of abstract classes? What are the characteristics of abstract classes?
  3. Why does Java have interfaces?
  4. Rules for the use of interfaces and definitions of methods in interfaces
  5. How do inner classes work?

abnormal

This content now in the development of IDE can only prompt has been done, more applications are their own development SDK or package components of the processing, clear exceptions, the execution order of the program, as well as the parent method throws exceptions, subclass rewriting throws exceptions rules.

A wrapper class

  • Make it clear why there is a wrapper class?
  • Familiar with packing and unpacking rules and the use of type conversion methods

The transformation method mentioned in the map, often used in development, we should practice more, at least the basic data type to try all once.

The following code is the focus of this section:

private static void compareDemo(a) {

    Integer num1 = 20;

    Integer num2 = integer.valueof (2021); * * Double and Float do not have this feature (constant pooling) */
    Integer num2 = 2021;

    Integer num3 = 20;
    Integer num4 = 2021;

    System.out.println("Is num1 equal to 20?" + (num1 == 20));
    // Automatic unpacking comparison
    System.out.println("Is NUM2 the same as 2021?" + (num2 == 2021));

    /** * The reason for different results: When the Integer ranges from -128 to 127, the two Integer objects are unpacked automatically before being compared. When the value exceeds this range, the two Integer objects are not */
    System.out.println("Is num1 equal to num3?" + (num1 == num3)); // 30 numbers are being compared
    System.out.println("Is num2 equal to num4?" + (num2 == num4)); // The object is comparing
}
Copy the code

string

This part of the content every Java developer must be familiar with, string interception, replacement, matching, capitalization conversion, search, splicing is the most common operations, the map listed methods must be skilled use.

A collection of

The key content in the map is marked, which belongs to the top priority. The set content in Java is also one of the most knowledge points asked in the interview. It must be kept in mind and skillfully used. ArrayList and HashMap are the most commonly used collections in development. It is important to understand their respective characteristics and common methods as well as unique methods. Most scenarios use both together, such as conditional filtering operations.

thread

The first step is to understand the default order of execution and the relationship between the main thread; Second, understand how the program executes when there are multiple threads. Once you understand these two issues, you will know when you need to create a thread.

(Also interview questions)

  • How a thread is created
  • The order in which threads are executed
  • Understanding of the synchronized keyword
  • Thread deadlock problem

IO

This section in the actual development combined with the network request, such as the download file, upload files, modify, face, etc., the specific apis used here are all the native, that is the underlying implementation logic is such, often in actual development, we will use the packaging framework, see the API implementation, inside framework will teach some method to us, Perhaps it is a sentence to achieve the download or upload; However, some of the scenes still require hand-stroking, so the core code of these principles should still be mastered, and the occasional interview will also require you to dictate the process.

Here are three ways to create a File with the File class, depending on your business:

/** * How to create File File **@paramParentPath Front-end path *@paramFileNamePath Back-end path */
private static void createFile(String parentPath, String fileNamePath) {
    File file1 = new File(parentPath + "\ \" + fileNamePath);
    System.out.println("file1 is exit ? " + file1.exists());

    File file2 = new File(parentPath, fileNamePath);
    System.out.println("file2 is exit ? " + file2.exists());

    File file3 = new File(new File(parentPath), fileNamePath);
    System.out.println("file3 is exit ? " + file3.exists());
}
Copy the code

conclusion

Jdk1.8 help document download: https://pan.baidu.com/s/12JHAlmwZbtBiJlPNZPlf3Q password: d5ya (this document is under the small make up from the Internet, if you have any other contents do not believe)

This is the most basic thing in Java. No matter how awesome the framework to contact behind, the bottom is inseparable from the support of the foundation.

Enumeration in Java (Enum)

It is relatively simple. Search for familiar writing methods and the subsequent project chapters will be applied.

Xiaobian specially created a public number: recommend learning Java, will share Java related content, and the original, welcome to search attention (attention is to send xiaobian selection of high-quality video tutorial), learn Java together!