"Crazy Java Lectures" 2

table of Contents

Array

class


Array

1. Java defines the form of array:

    type[] arrayName; (commonly used)

    type arrayName[ ];

2. An array is a reference variable, so when you use it to define a variable, it only means that a reference variable is defined (that is, a pointer is defined). This reference variable does not point to any valid memory, so you cannot specify an array when defining an array In addition, this array cannot be used, and can only be used after the array is initialized.

3. Array initialization: allocate memory space for array elements and assign initial values.

    Two ways of initialization:

    (1) Static initialization: The programmer determines the initial value of the array elements, and the system determines the length of the array;

              Syntax format:

               arrayName = new  type[ ] {element1,element2,element3......}

                Or simply: type[] arrayName = {element1,element2,element3......}

    (2) Dynamic initialization: The programmer determines the length of the array, and the system determines the initial value.

            Syntax format:

              arrayName = new    type[length];

4. Distinguish between stack memory and heap memory:

    Stack memory usually refers to when a method is executed, this method will create a memory stack, the variables in the method are placed on this stack, when the method ends, the stack will be destroyed.

    Heap memory refers to when a program creates an object, the object is saved in the data area for repeated use. Only when there is no reference variable to refer to it, the system's garbage collector will collect it at an appropriate time.

class

[Modifier] Type member variable name [=default value];

Modifiers: can be omitted, or can be written as public, protected, private, final, static;

  1. For a class definition, it can contain the three most common members: constructors, member variables, and methods. Note: Members modified by static cannot access members without static modification.

  2. Java uses the new keyword to call the constructor to return an instance of this class. If a class does not have a constructor, this class usually cannot create an instance. If the programmer does not write a constructor, the system automatically provides a default constructor. If the programmer does, the system will no longer provide it.

  3. The syntax format for defining member variables:

    4.static:

        Static is a special keyword, and its modified members indicate that it belongs to the class itself, not to a single instance of this class. Therefore, variables and methods modified by static are usually called class variables and class methods; variables that are not modified by static And methods are called instance variables and instance methods.

    5. Constructor:

        Syntax format:

        [Modifier] Constructor name (parameter list)

    {

        //Executive body

    }

  • Modifier: can be omitted, or one of public, private, protected

  • Constructor name: must be the same as the class name

  • Formal parameter list: The format of the formal parameter list of the definition method is exactly the same

Note: The constructor can neither define the return value type, nor use void to declare that the constructor has no return value! ! !

 

 

 


Alas, I wanted to write a meeting again. I don’t have enough energy and I’m a little sleepy. I won’t do it anymore. I’ll talk about it next time. Actually, I’ve read these things before, but because the exam is a bit rushed and many things are swallowed up, I want to use the holiday to review. For a moment, it doesn't hurt. Unfortunately, I wanted to make the first version of Gobang for everyone to see, but because I'm not very familiar with eclipse, EditPlus won't work. If you set this flag, you will definitely show it if you have a chance! Good night, my few readers~

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113985038