Dabai became a siege lion of Java software on the nineteenth day (java construction methods, constructors, concepts of objects and references, parameter passing)

Construction method

About the construction method in the java class:

1. The construction method is also called constructor/constructor/Constructor

2. The grammatical structure of the construction method:
[修饰符列表] 构造方法名(形式参数列表){ 构造方法体; }

3. Review the grammatical structure of ordinary methods:
[修饰符列表] 返回值类型 方法名(形式参数列表){ 方法体; }

4. For the constructor, the "return value type" does not need to be specified, and void cannot be written. As long as void is written, this method becomes an ordinary method.

5. For the construction method, the method name of the construction method must be consistent with the class name.

6. What does the construction method do?

  • The meaning of the construction method is that an object can be created by calling the construction method.

7. How should the constructor be called?

  • The ordinary method is called like this: when there is static in the 类名.方法名(实参列表)method modifier:, when there is no static in the method modifier list:引用.方法名(实参列表)
  • new 构造方法名(实参列表)

8. After the constructor call is executed, is there a return value?

  • Each construction method actually has a return value after the execution ends, but this "return value;" statement does not need to be written. The java program automatically returns the value when the construction method ends.
  • And the return value type is the type of the class where the constructor is located. Since the return value type of the constructor is the class itself, the return value type does not need to be written.

9. Comment and uncomment: Ctrl + /, multi-line comment: ctrl + shift +/\

10. When no construction method is defined in a class, the system provides a parameterless construction method for the class by default. This construction method is called the default constructor.

11. When a class clearly defines its constructor, the system no longer provides a default constructor for this class by default. It is recommended to manually provide a parameterless construction method for the current class during development. Because the parameterless construction method is too common.

The role of the construction method

  • 1. Create an object
  • 2. While creating the object, initialize the memory space of the instance variable.

Instance variables of member variables belong to team-level variables. Such variables must have objects before they can have instance variables.

When the instance variable is not manually assigned, the system assigns it by default. Then when is the system default assignment completed? Is it during class loading?

  • No, because only code fragments are loaded when the class is loaded, and the object has not been created yet. So the instance variable is not initialized at this time.
  • In fact, the memory space of instance variables is opened up and initialized during the execution of the construction method.
  • When the system assigns by default, it is also done during the execution of the construction method.

Objects and references

Objects and references

  • Object : The memory space currently opened up in the heap memory using the new operator is called an object.
  • Reference : It is a variable, not necessarily a local variable, but also a member variable. The reference saves the memory address and points to the object in the heap memory.
  • All access to the data related to the instance needs to be accessed in “引用.”a way, because the object can only be found by reference.
  • There is only a null reference, a null pointer exception will occur when accessing data related to the object instance.

Parameter passing

The main research and study is that when the method is called, the problem of parameter transmission is involved. How to transfer data?

  • Pass by value

int i = 10;
int j = i; i is passed to j, in fact only the 10 saved in the i variable is passed to j, j is actually a brand new memory space

User u = 0x1234;
User u2 = u;
u is passed to u2, actually assigning the value of 0x1234 to u2, u and u2 are actually two different local variables, but these two variables point to the heap memory The same java object.

The problem of parameter transfer involved in method call in java language

The parameter passing actually passes the specific value saved in the variable

Example 1:

public class Test01()
{
    
    
	public static void main(String[] args){
    
    
		
		//int i = 10;
		//add(i)等同于:add(10)
		int i = 10;
		add(i);//add方法调用的时候,给add方法传递了一个变量i,实际上传递的是变量中保存的具体值
		System.out.println("main-->"+ i );
	}

	public static void add(int i){
    
    
		i++;
		System.out.println("add-->"+ i);
	}
}

Compilation result:
Compilation result
Example 2:

public class Test02(){
    
    
 public static void main(String[] args){
    
    
        User u = new User(20);
      
       //User u =0x1234;
       //add(u)等同于:add(0x1234)
        add(u);//传递u给add方法的时候,实际上传递的是U变量中保存的值,只不过这个值是一个java对象的内存地址
        System.out.println("main-->"+ u.age);
    }

    public static void add(User u ){
    
    
        u.age++;
        System.out.println("add-->"+ u.age);
    }
}

class User
{
    
    
    int age;
    public User(int i){
    
    
        age = i;
    }
}

Compilation results:
Insert picture description here
final conclusion:

When the method is called, it involves the issue of parameters. When passing, Java only follows a grammatical mechanism, which is to pass the "value" stored in the variable, but sometimes the value is a literal value of 10, sometimes It is the memory address 0x1234 of another java object.

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/113062589