The difference between construction method, null pointer anomaly, basic data type and reference data type as parameters in Java

1. Constructor

1. The concept of construction method

The construction method is a special method in the class. The creation of the object and the initialization of the object properties are completed by calling the construction method.

2. Definition of construction method

【Modifier list】Construction method name (formal parameter list) {Construction method body;}

  • The constructor name is the same as the class name
  • The constructor is used to create the object and complete the property initialization operation
  • The return value type of the constructor does not need to be written, even void cannot be written
  • The return value type of the constructor is actually the type of the current class
  • Multiple construction methods can be defined in a class, and these construction methods constitute method overload

Note : When a class does not explicitly define any construction method, the system will provide a parameterless construction method (default constructor) by default. After the displayed definition construction method, the system will no longer provide a parameterless construction method. and so,It is recommended to write the parameterless construction method manually, because the frequency of the parameterless construction method is higher

code show as below:

public class Constructor {
    
    
    public static void main(String[] args) {
    
    
        Date d1=new Date() ;
        d1.show();
        Date d2=new Date(2020);
        d2.show();
        Date d3=new Date(2020,11);
        d3.show();
        Date d4=new Date(2020,11,26);
        d4.show();

    }

}
class Date{
    
    
    int year;
    int month;
    int day;
    Date(){
    
    
        System.out.println("无参构造方法被调用!");
    }
    Date(int year){
    
    
        this.year=year;
        System.out.println("带有参数year的构造方法被调用!");
    }
    Date(int year,int month){
    
    
        this.year=year;
        this.month=month;
        System.out.println("带有参数year,month的构造方法被调用!");
    }

    Date(int year,int month,int day){
    
    
        this.year=year;
        this.month=month;
        this.day=day;
        System.out.println("有参构造被调用了!");
    }
    public void show(){
    
    
        System.out.println("今天的日期是:"+year+":"+month+":"+day);
    }
}

Operation result:
Insert picture description here
  Through this piece of code, we can see that multiple construction methods can be defined in a class, and the construction method supports the overload mechanism .Which construction method is called depends on which construction method the actual parameter list passed during the call matches.

  Although the construction method does not write any type in the return value type, it will actually return the memory address of the object in the heap memory after its execution. At this time, the variable can be defined as the memory address of the receiving object. This variable is what we call "References". The above programs d1, d2, d3, d4 are all references, and we can access the memory of the object through this reference.

  In the above code, we can see that if no value is assigned to the attribute, 0 will be output by default. This is becauseThe attribute is not assigned a value in the constructor, the system will assign a default value.

  When the class is loaded, the space of the instance variable is not initialized, and the assignment operation is performed only when the constructor is executed. This is whyInstance variables cannot be accessed using class namess reason.

Two ways to assign values ​​to member variables:

1. get\set method
2. construction method

  When we learn about encapsulation features in Java, we use the private keyword to modify the properties that need to be protected, and then provide public get\set methods for this private property , where the set method is used to modify the value of the property, the get method Used to read the value of the attribute. Then some people may have this question: the attribute has been assigned a value in the constructor, why is it necessary to provide the get\set method.

  We need to know that these are two completely different moments. The attribute assignment in the constructor is inWhen creating objectsFinished . After the object is created, the properties may still be modified. If you want to modify the value of the property later, you must call the set method at this time.

2. Null pointer exception

Code:

public class Nullpointer {
    
    
    public static void main(String[] args) {
    
    
        balloon a =new balloon("红色","氢气");
        a.show();
        //置空引用!
        a=null;
        //发生空指针异常
        a.show();
    }
}
class balloon{
    
    
    String color;
    String gas;
    balloon(){
    
    

    }
    balloon(String color,String gas){
    
    
        this.color=color;
        this.gas=gas;
    }
    public void show(){
    
    
        System.out.println("该气球的颜色是"+color+"气体是"+gas);
    }
}

Operation result:
Insert picture description here
when aWhen a null reference is used to access instance variables, java.lang.NullPointerException will occur.. This is because when ball=null is executed, it means that the "reference balloon" no longer saves the memory address of the java object. In other words, the java object in the heap memory cannot be found through the balloon reference. In this case, a null pointer exception will occur. .

Third, the transmission of parameters when the method is called

Calling parameters are passed to the method, this process is the process of assignment, that is to say, parameter passing is exactly the same as the "assignment rule".
Code:

public class Transitive {
    
    
    public static void main(String[] args) {
    
    
        //基本数据类型的赋值
        int a=100;
        int b=a;
        //引用数据类型的赋值
        Bird b1=new Bird("鹦鹉");
        Bird b2=b1;
        //输出结果
        System.out.println("b="+b);
        System.out.println("b1=" + b1);
        System.out.println("b2="+b2);
    }

}
class Bird{
    
    
    String name;
    Bird(String name){
    
    
        this.name=name;
    }
}

Operation result:
Insert picture description hereMemory change graph:
Insert picture description here
Insert picture description here
  Through the memory graph, we can see that the "assignment" operation has nothing to do with the data type of the variable. Whether it is the basic data type or the reference data type, it will always talk about the "value" stored in the variable. Make a copy, and then assign the copied "value" to it. The difference between them is that the basic data type has nothing to do with the objects in the heap memory. If it is a reference data type, because the value passed is the memory address of the java object, it will cause two references to point to the java object in the same heap memory. In other wordsThe basic data type is passed the value , the reference data type is passed the address value

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/110160632