Fundamentals of Java programming language 05: Java classes and objects

catalogue

1. Definition and composition of class

1.1 definition of class

2.2 use of class

2. Classes, objects and references

Type 2.1

2.2 object

2.3 references

3. Class knowledge expansion

3.1 initial value of member variable

3.2 define your own reference in the class

3.3 array of classes

1. Definition and composition of class

1.1 definition of class

Class is used to describe the same kind of things. It achieves the purpose of creating new data types by encapsulating other data types. The class can contain basic data types and non basic data types

public class Car {
    int speed;
    String color;
    String name;
    String direction;
}

Description 1: member variable

You can define any number of variables of different types inside the class as the attributes of this kind of things. This attribute is called member variable

Member variables are also called field s, and the naming rules are the same as variable names

Note 2: storage of class

① Java requires that the file name of the class modified by public must be consistent with the class name, so the Car class in the example must be stored in Car Java file

Because of this provision, there can only be one public modified class in a source file. Otherwise, if you define a public class that does not have the same name, the file name does not match; It is illegal to define a class with the same name

② The source files of Java language are organized by classes. A source file contains 1 public class + 0 or more non-public classes

Description 3: public class

① public is an access modifier used to control access rights

② The class modified by public can be publicly used by other classes

Note 4: naming habits of class names

① It is customary to use one or more capitalized words to represent the class name

② This is just a naming convention, not a grammatical requirement

Note 5: empty classes can be defined

public class EmptyClass {
    
}

2.2 use of class

In Java, all but eight basic data types are used by reference (including classes and arrays)

public class UseCar {
    public static void main(String[] args) {
        Car referenceToCarObject;
        referenceToCarObject = new Car();

        referenceToCarObject.speed = 100;
        referenceToCarObject.color = "red";
        referenceToCarObject.name = "Honda";
        referenceToCarObject.direction = "NanJing";
        System.out.println("speed = " + referenceToCarObject.speed);
        System.out.println("color = " + referenceToCarObject.color);
        System.out.println("name = " + referenceToCarObject.name);
        System.out.println("direction = " +
        referenceToCarObject.direction);
    }
}

Description 1: use the point operator in Java to access the properties in the class

Note 2: when creating an object of a class, the parentheses added in new Car() are actually function calls, and the constructor of the class is called

2. Classes, objects and references

Car referenceToCarObject;
referenceToCarObject = new Car();

The relationship between classes, objects and references in the above code fragment is shown in the figure below,

Type 2.1

① Class is an abstraction. It abstracts the characteristics of a class of things and represents them through appropriate data types

② You can think of a class as a template

2.2 object

① Object is the concretization of the abstract class. An object of Car class is created through the new Car() statement

② We also call an object an instance of a class

2.3 references

① The Car referenceToCarObject statement defines a reference that can point to an object of type Car

② An object must be used by reference because it has no name and is just an entity. Therefore, the reference to an object is the name of the object (multiple references can point to the same object)

③ The type of the reference must match the type of the object pointed to (the matching here involves the following knowledge about class inheritance)

Note 1: there are only two types in Java, basic data types and classes, and basic data types are not referenced

Note 2: the default value of the reference is null, which means that the reference does not point to any object

public class UseCar {
    public static void main(String[] args) {
        Car referenceToCarObject;
        referenceToCarObject = new Car();
        System.out.println("speed = " + referenceToCarObject.speed);
        System.out.println("color = " + referenceToCarObject.color);
        System.out.println("name = " + referenceToCarObject.name);
        System.out.println("direction = " +
        referenceToCarObject.direction);
    }
}

If an object member is accessed through a reference with a null value, the NullPoniterException problem is triggered. Therefore, when you are not sure whether a reference is null, you can compare it with null and access it only when it is not null

Note: the above conclusion about "the default value of reference is null" is verified by three String type references in the Car class

In principle, it is the default initialization of reference type member variables when constructing objects

So what is the value of the reference type already pointed to?

public class UseCar {
    public static void main(String[] args) {
        Car referenceToCarObject;
        referenceToCarObject = new Car();
        System.out.println("referenceToCarObject = " +
        referenceToCarObject);
    }
}

The value of the visible reference is a pointer on the heap, that is, the object is stored in the heap space

Description 3: array is a special class

Array types are also classes and need to be accessed by reference, but they are different from ordinary classes as follows,

① There is no need to define the array source file

② The class name of the array is the type, with brackets, such as int []

③ For the same type of array, the size of each array object can be different, that is, the memory occupied by each array object can be different, while the size of ordinary class objects is fixed and the same

Example:

int[] a = new int[10];
a = new int[20];

That is, you can point to arrays of the same type but different sizes by reference, because they belong to the same type (int [] type)

3. Class knowledge expansion

3.1 initial value of member variable

When defining a class, you can set the initial value of the member variable, so that the initial value of the member variable in the generated object is no longer the initial value provided by the system according to the type

public class Car {
    int speed = 50;
    String color = "black";
    String name = "none";
    String direction = "north";
}

public class UseCar {
    public static void main(String[] args) {
        Car referenceToCarObject;
        referenceToCarObject = new Car();

        System.out.println("speed = " + referenceToCarObject.speed);
        System.out.println("color = " + referenceToCarObject.color);
        System.out.println("name = " + referenceToCarObject.name);
        System.out.println("direction = " +
        referenceToCarObject.direction);
    }
}

 

3.2 define your own reference in the class

In a class definition, you can include your own references

public class Node {
    Item item;
    Node next;
}

This is the node type definition of one-way linked list in Java, which contains a reference to the next node of the same type

Note: the reason why you can define your own reference in a class in Java is that the memory occupied by the reference is fixed, which is related to the JDK version. For 64 bit JDK, the reference size is 8B

This is the same as the principle that the size of pointers in C language is fixed. Therefore, in C language, you can also include pointers to your own type in the structure

3.3 array of classes

We can also use the type of class to define arrays

Car[] cars = new Car[5];

At this time, cars is the reference of Car type array. In this array, each member is a reference of Car type. The default value here is null

In other words, new Car[5] only creates a reference array. The value of each reference in this array is null, and no object of Car class is created

Added by Cheap Commercial on Fri, 04 Mar 2022 22:58:10 +0200