When object-oriented, hello must love the world? [summary of the most popular Java foundation in history (6000 words)]

πŸ“’ Blog home page: πŸ€ Jiu Jiu uncle wineπŸ€
πŸ“’ Welcome to praise πŸ‘ Collection ⭐ Leaving a message. πŸ“ Welcome to discuss! πŸ‘
πŸ“’ This article was originally written by [Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu Jiu πŸ™‰πŸ™‰πŸ™‰
πŸ“’ Since the blogger is learning Xiaobai, there will inevitably be mistakes. If you have any questions, please leave a message in the comment area. Thank you very much! ✨ Personal home page
πŸ“– Boutique column (updated from time to time) [JavaSE] [MySQL][LeetCode][Web][operating system]

πŸ“– Boutique article (pure JavaSE implementation of library management system < including refined notes >)

catalogue

Write in front

Do you really understand our main function!?

Who knows the romance of the main function?

When you say hello to the world, the world is also facing you~  

The above principle

supplement

How to say hello before she (main method) says hello world?

How to output "Hello, World" before the main() method is executed

3. What is the order of Java program initialization [interview required! ~]

Theoretical foreshadowing

It is easy to say,show me your code.

Define parent class Base~

Define subclass Derived~

Final summary

Last last

Write in front

Looking back, I chose Java, let alone the wind and rain, and twisted my head forward. Sometimes I didn't understand that the problems were gradually shelved. Over time, the foundation was not strong and the earth shook. Recently, I turned back to look at the basic knowledge of Java and looked at it from different angles. I gained a lot. I remember it.

Because of my limited level, I'm just learning Xiaobai. The understanding of knowledge is inevitably inappropriate. There may be mistakes in the code. If you have any questions, please leave a message in the comment area. Thank you very much!

It's not easy to create. Please support the third company~

Java learning journey, we study together and make progress together~

Do you really understand our main function!?

I believe that after most people choose a programming language, the first contact is to output HelloWorld!?

Java object-oriented romance ~ only Java program apes understand. But in the process of continuous learning and deepening!

No matter in the compiler console, or in the fancy output on the screen, or in the small black box of cmd, or on the web page.

Who knows the romance of the main function?

Copy the following line of code

Paste into text file

public class A {
    public static void main(String[] args) {
        System.out.println("HelloWorld!~");
    }
}

Build a: A.java Java file (Java source file)

​

Open cmd above

​

We use the javac command on the command line to compile the java source file just now

A Java. Net file is generated in the visible folder class Class file (compiled file) binary bytecode file~

Β 

I found it was garbled when I opened it.

We then use the java command to convert java Class , load it on the JVM to run

Β 

When you say hello to the world, the world is also facing you~  

The above set of operation flow is just to be familiar with the java and javac commands in cmd

Next, copy the following code and create a new B.java file

public class B {
    static public synchronized final void main(String[] args) {
        System.out.println("HelloWorld!");
        for (String string : args) {
            System.out.println(string);
        }
    }
}

This time, when we run B.class using the java command

Using java B object oriented programming

Β 

HelloWorld!
object
oriented
programming

You passed through my world and left a Hello, and I was staring at you~

First knowledge of object-oriented language, HelloWorld! So is romance

The above principle

main is the special method name recognized by the JVM and the entry method of the program. The string array parameter args provides a means for developers to interact with the program in the command line state~

supplement

1) Definition java files can refer to my previous articles, Change file extension

2) In addition, some friends should have noticed that our main() method has other definition formats. There is no sequential relationship between public and static. The position can be changed, or the final modifier and synchronized modifier can be added~

static public synchronized final void main(String[] args)

How to say hello before she (main method) says hello world?

How to output "Hello, World" before the main() method is executed

As we all know, in the Java language, main is the entry method of the program. When the program runs, the main method is loaded first,

But does this mean that the main method is the first module to be executed when the program runs?

The answer must be No.

Because the object-oriented characteristics of Java determine that the class is the most basic unit in Java. When the class is loaded, it will enter the main() method along the entrance of the main function in the class. Execute the contents of the main() method~

public class C {
    static{
        System.out.println("Hello");
    }
    public static void main(String[] args) {
        System.out.println("HelloWorld");
    }
}

The code is as above, and the result is as follows~

Β 

3. What is the order of Java program initialization [interview required! ~]

Theoretical foreshadowing

Through the above tests, we found that the order of class loading in Java is the highest priority

The initialization of Java programs generally follows three principles (decreasing priority)

1) Static objects (variables) (static code blocks) take precedence over non static objects (variables) (initial code blocks)

Among them, static content is initialized only once! Non static content may be initialized multiple times.

2) The parent class takes precedence over the child class for initialization

3) Initialize in the order in which member variables are defined

Among them, the variables and code blocks of the same level (i.e. both static and non static) depend on the definition order, who will execute first and then

The default value rule is open and automatically supplemented, such as class variables and member variables in the class. The default value of Int basic type is 0;

class A{
int anInt;//The system will automatically make up 0 for him; This is called the default rule
}

class B{
    int anInt1=1024;//This is called initialization rule

}

It is easy to say,show me your code.

Define parent class Base~

package com.zdx.daily.java0101.test;

/**
 * @program: daily.study
 * @description: Parent class for testing
 * @author: SmallRedSun
 * @create: 2022-01-01 15:12
 **/
public class Base {
    static int anStaticInt = StaticIntMethod();

    private static int StaticIntMethod() {
        System.out.println("1------>Initial value of parent static variable: class variable");
        return 1;
    }

    static{
        System.out.println("2------>Parent static code block~Sandwiched between static variables one and three~");
    }

    static int getAnStaticInt=sMethod();

    private static int sMethod() {
        System.out.println("3------>Initial value of parent static variable: class variable");
        return 3;
    }



    int anInt = intMethod();

    private int intMethod() {
        System.out.println("4------>Parent class initialization rule: attribute");
        return 4;
    }

    {
        System.out.println("5------>Parent class construction code block (normal code block)~)~Sandwiched between static variables one and three~");
    }

    private int intMethod1() {
        System.out.println("6------>Parent class initialization rule: attribute");
        return 6;
    }

    protected Base(){
        System.out.println("7------>Parent constructor (constructor)");
    }

}

Define subclass Derived~

package com.zdx.daily.java0101.test;

/**
 * @program: daily.study
 * @description: Test subclass, inherited from Base class
 * @author: SmallRedSun
 * @create: 2022-01-01 15:21
 **/
public class Derived extends Base{
    static int anStaticInt = StaticIntMethod();


    private static int StaticIntMethod() {
        System.out.println("1------>Initial value of subclass static variable: class variable");
        return 1;
    }

    static{
        System.out.println("2------>Subclass static code block~Sandwiched between static variables one and three~");
    }

    static int getAnStaticInt=sMethod();

    private static int sMethod() {
        System.out.println("3------>Initial value of subclass static variable: class variable");
        return 3;
    }



    int anInt = intMethod();

    private int intMethod() {
        System.out.println("4------>Subclass initialization rule: attribute");
        return 4;
    }

    {
        System.out.println("5------>Subclass construction code block (common code block)~)~Sandwiched between static variables one and three~");
    }

    private int intMethod1() {
        System.out.println("6------>Subclass initialization rule: attribute");
        return 6;
    }

    protected Derived(){
        System.out.println("7------>Subclass constructor (construction method)");
    }

    public static void main(String[] args) {
        int a=1024;
        System.out.println(a+"Invoked main function~~");
        System.out.println("Take this as the dividing line~~~The above static are executed with the class loaded and the entry is found main The method was carried along~~");
        System.out.println("Because of your main Method contains static Modifier, which can the class name.main Call, so static");
        System.out.println("So the arrangement for you is clear. First, load the static attribute code block for you!");
        System.out.println("Next, if there is another object, it should start from 4 of the parent class~");
        System.out.println("-------β€”Split line -————————————————");
        Derived derived = new Derived();
        System.out.println("Because the static can only be loaded once, the above new The object has been loaded once before!");
        System.out.println("So start again new The object still starts with 4~If not, please wait and see~");
        System.out.println("-------β€”Split line -————————————————");
        System.out.println("Next new An object~");
        Derived derived1 = new Derived();
    }

    public static void main1(String[] args) {
        System.out.println("-------β€”Split line -————————————————");
        System.out.println("The above should be from parent class 1~3 With subclass from 1~3,load main Methods are essential~");
        System.out.println("-------β€”Split line -————————————————");
        System.out.println("The next step is to directly new Object, parent class before child class, from 4 to 7!~");
        Derived derived = new Derived();
        System.out.println("-------β€”Split line -————————————————");
        System.out.println("The instantiation of a subclass object is actually performed by the parent object quietly loading the construct~Because the parent class constructor is the last");
        System.out.println(
                "This also explains why the parent class has parameters and the child class needs parameters super Explicitly call the constructor of the parent class"
        );
        System.out.println("In fact, the construction method is execution new Object is the last step~Go!~");

    }

}

Copy the above two pieces of code!

First restore the above main method to normal running (the following is changed to main1)

Then change the above to main1 and run the following main method~

Combined with the notes, observe several times. The initialization sequence of Java programs is easy to grasp

Final summary

Turn around and look at the first time I knew Java. It's a long time since I correctly used javac and Java commands to output HelloWorld in a cmd! Now look again, but there were too many jdks downloaded at the beginning, 11 of 1.8. Because there are too many jdks. Path and home -- path is confused. More disassembly and assembly times, and then configure JDK and path, etc. ~ it's easy to find out what's in your pocket and look at the pattern~

When you encounter difficulties, really don't think about going around at the first time. Difficulties are like springs.

Recently, classes were suspended and isolated during the epidemic in Xi'an, and they gradually realized that only systematic learning can learn real things.

Because of my limited level, I'm just learning Xiaobai. The understanding of knowledge is inevitably inappropriate. There may be mistakes in the code. If you have any questions, please leave a message in the comment area. Thank you very much!

It's not easy to create. Please support the third company~

Go back to the system Java learning journey, we learn together and make progress together~ Let's look forward to the future together!

Last last

Time goes by and stars change. Please believe it
Love and hope always spread faster than the virus
Please believe
China's power of "one side is in trouble and all sides support"
Please believe
When the winter is over, the Star River will be bright
We will finally use courage and perseverance
Illuminate the same mission

Mountains and rivers share wind and rain
The sun and the moon shine tomorrow
We fight the epidemic with one heart
It will bring all the hardships and joys, and the mountains and rivers will be safe

The world is safe, everything goes well, and there is no worry about long-term happiness

Keywords: Java Back-end

Added by amrigo on Tue, 04 Jan 2022 10:27:41 +0200