(Java Foundation) keywords

keywords

Does Java have goto?

  • goto is a reserved word in Java and is not used in the current version of Java.

What's the use of final?

用于修饰类、属性和方法;
  • Classes modified by final cannot be inherited
  • A method modified by final cannot be overridden
  • Variables modified by final cannot be changed. What is final modified and immutable is the reference of the variable, not the content pointed to by the reference. The content pointed to by the reference can be changed

final finally finalize difference

  • Final can modify classes, variables, and methods. Modified classes indicate that the class cannot be inherited, modified methods indicate that the method cannot be overridden, and modified variables indicate that the variable is a constant that cannot be reassigned.
  • finally generally works in the try-catch code block. When dealing with exceptions, we usually put the code method that must be executed in the finally code block, indicating that the code block will be executed regardless of whether an exception occurs. It is generally used to store some closed resources. code.
  • finalize is a method that belongs to the Object class, and the Object class is the parent class of all classes. This method is generally called by the garbage collector. When we call the System.gc() method, the garbage collector calls finalize (), recycling garbage, the final judgment of whether an object can be recycled.

usage of this keyword

  • this is an object of itself, representing the object itself, which can be understood as: a pointer to the object itself.

  • The usage of this in java can be roughly divided into three types:

    • 1. Ordinary direct reference, this is equivalent to pointing to the current object itself.

    • 2. The name of the member of the formal participation is the same, use this to distinguish:

      public Person(String name, int age) {
              
              
          this.name = name;
          this.age = age;
      }
      
    • 3. Reference the constructor of this class

      class Person{
              
              
          private String name;
          private int age;
          
          public Person() {
              
              
          }
       
          public Person(String name) {
              
              
              this.name = name;
          }
          public Person(String name, int age) {
              
              
              this(name);
              this.age = age;
          }
      }
      

Usage of the super keyword

  • super can be understood as a pointer to its own super (parent) class object, and this super class refers to the parent class closest to itself.

  • super also has three usages:

    • 1. Ordinary direct quotes

      Similar to this, super is equivalent to a reference to the parent class of the current object, so that super.xxx can be used to refer to members of the parent class.

    • 2. When the member variable or method in the subclass has the same name as the member variable or method in the parent class, use super to distinguish

      class Person{
              
              
          protected String name;
       
          public Person(String name) {
              
              
              this.name = name;
          }
       
      }
       
      class Student extends Person{
              
              
          private String name;
       
          public Student(String name, String name1) {
              
              
              super(name);
              this.name = name1;
          }
       
          public void getInfo(){
              
              
              System.out.println(this.name);      //Child
              System.out.println(super.name);     //Father
          }
       
      }
      
      public class Test {
              
              
          public static void main(String[] args) {
              
              
             Student s1 = new Student("Father","Child");
             s1.getInfo();
       
          }
      }
      
    • 3. Refer to the parent class constructor

      • super (parameter): Call a constructor in the parent class (should be the first statement in the constructor).
      • this (parameter): Call another form of constructor in this class (should be the first statement in the constructor).

The difference between this and super

  • super: It refers to the members of the direct parent class of the current object (used to access member data or functions in the hidden parent class in the direct parent class, when the base class and the derived class have the same member definition, such as: super. variable name super .Member function data name (actual parameter)
  • this: It represents the name of the current object (where ambiguity is likely to occur in the program, this should be used to indicate the current object; if the member data in the formal participation class of the function has the same name, this is required to indicate the member variable name)
  • super() is similar to this(), the difference is that super() calls the constructor of the parent class in the subclass, and this() calls other constructors of the class in the class.
  • Both super() and this() need to be placed in the first line of the constructor.
  • Although you can call one constructor with this, you cannot call two.
  • This and super cannot appear in a constructor at the same time, because this will inevitably call other constructors, and other constructors must also have super statements, so if there are the same statements in the same constructor, it will be lost. The meaning of the statement, the compiler will not pass.
  • Both this() and super() refer to objects, so neither can be used in a static environment. Including: static variable, static method, static statement block.
  • Essentially, this is a pointer to this object, while super is a Java keyword.

The main meaning of static existence

  • The main significance of static is to create domain variables or methods independent of specific objects. So that even without creating an object, you can use properties and call methods !
  • Another key function of the static keyword is to form static code blocks to optimize program performance . The static block can be placed anywhere in the class, and there can be multiple static blocks in the class. When the class is first loaded, each static block will be executed in the order of the static blocks, and will only be executed once.
  • The reason why the static block can be used to optimize program performance is because of its characteristics: it will only be executed once when the class is loaded. Therefore, in many cases, some initialization operations that only need to be performed once are placed in static code blocks.

Uniqueness of static

  • 1. Variables or methods modified by static are independent of any object of the class, that is, these variables and methods do not belong to any instance object, but are shared by instance objects of the class .

How do you understand the phrase "shared by instance objects of the class"? That is to say, a static member of a class belongs to everyone [Everyone refers to multiple object instances of this class, we all know that a class can create multiple instances! ], all class objects are shared, not like member variables are their own [self refers to a single instance object of this class]... I think what I have said is very general, do you understand?

  • 2. When the class is loaded for the first time, it will load the part modified by static, and only load and initialize the class when it is used for the first time. Note that it needs to be initialized for the first time. Need to be reassignable.
  • 3. The static variable value is allocated space when the class is loaded, and will not be reallocated when the class object is created later. If you assign a value, you can assign it arbitrarily!
  • 4. Variables or methods modified by static are prior to objects, that is to say, after a class is loaded, it can be accessed even if no object is created.

static application scenarios

  • Because static is shared by instance objects of the class, if a member variable is shared by all objects, then this member variable should be defined as a static variable .
  • Therefore, the more common static application scenarios are:

1. Modified member variables 2. Modified member methods 3. Static code blocks 4. Modified classes [only internal classes can be modified, that is, static internal classes] 5. Static package import

static notes

  • 1. Static can only access static
  • 2. Non-static can access both non-static and static.

Guess you like

Origin blog.csdn.net/QRLYLETITBE/article/details/129644008