• preface

JAVA data types – fall into two broad categories: primitive types and object types. Accordingly, variables have two types: primary and reference.

Basic data types are stored in stacks. int a = 3; int b =3; Variable A looks for the literal 3 and doesn’t find it, clearing space in the stack. The variable B directly finds the literal 3, shared. If YOU set a equal to 4, then b won’t be equal to 4, it’ll be equal to 3.

Reference data types: If an object (or array) is used as an argument in a method, the argument is passed a reference to the object (address) when the method is called, that is, the actual argument passes a reference to the object (address) to the formal argument when the method is called. This is the actual parameter and the formal parameter point to the same address, namely the same object (array), when the method is executed, the change to the formal parameter is actually the change to the actual parameter, this result is retained after the call.

public static void main(String[] args) {
        TestDemo demo = new TestDemo(1."Test");
        changeData(demo);
        //TestDemo{code=11, name=' TestDemo '}
        System.out.println(demo);
    }

    private static void changeData(TestDemo demo) {
        demo.setCode(11);
        //The value new TestDemo(2," test2 ") assigned to 'demo' is never used
        demo = new TestDemo(2."The test 2");
    }
Copy the code
  • subsequent

Both the base data type and the parameters that create the new reference type are operated on in a method without changing the data of the arguments. Changing a value of a reference data type actually synchronizes the change.