Java in the value passed to the "passing by reference" - some ideas

When looking at these two days to see the Java method and pass the values ​​passed by reference, to pass values ​​we can all understand, very simple, but for the passed by reference, for those of us who learned of c ++ that was not in high spirits yet, write a function to directly use the "&" operator directly change the value of the parameter - how convenient ah! !

BUT!!

Java is not passed by reference, only values ​​passed

  • Arguments: actual arguments, is prepared in advance and variable assignment completed. Allocated on the stack. If it is dispensed directly onto the basic types of the stack, if it is assigned a reference pointer to the object referenced type, store a pointer to the stack space allocated on the heap itself.
  • Parameter: the parameter form, copy distribution method call arguments on the stack.
  • Value is passed: method call, passing it the actual parameter values ​​to the corresponding formal parameters, the received parameter is a copy of the original value, and the two equal variables stored in memory
  • Pass a reference: the argument is the address of the transfer method call to the corresponding parameter, and parameter arguments point to the same content 

So how do we change the data you want to change it?

In Java, there are two major types of data:

1, the basic data types: numeric types comprising (integer, floating point), character type (char), the type of logic (boolean)

2, reference data types: a class, interface, array

We carry out the basic data type of the operation, then only change the value of the parameter, the argument does not change, and if we operate for reference data types, you can change the data we want to change (due to address directly the operation)

We do not like c ++ as a bohemian in Java, such a simple operation memory for Java, is simple elements, so people need to reference a headache naturally instead of using other methods -

Here are two ways for me to see recently for the way we want it to itself to change:

First, the use of array operation.

We know that in c ++, arrays and pointers are in fact the same as in Java seems to retain this feature, so we can work directly for the array, so that the changes reflected in the actual top.

code show as below:

public class helloworld {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        for(int i=0 ; i<5 ; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        change(arr);
        for(int i=0 ; i<5 ; i++) {
            System.out.print(arr[i]+" ");
        }
}
    public static void change(int arr[]) {
        for(int i=0; i<5; i++) {
            if(arr[i]%2 == 0) {
                arr[i]*=2;
            }
        }
    }
}

 

(Forgive the lazy blogger, the same item repeatedly used a lot of times ..... cry Liao orz)

Of course, we can not use an array of time:

At this time there is a more cumbersome way - to create the object. . .

Second, using the operation object.

Since the object is the reference data types, we can change the value of the data directly, as follows:

 

class number{
        private int a;
        private int b;
        public number(int num1,int num2) {
            this.a=num1;
            this.b=num2;
        }
        public void ShowNumber() {
            System.out.println("a = "+a+" b = "+b);
        }
        public void Swap() {
            int temp = this.a;
            this.a = this.b;
            this.b = temp;
        }
}

public class helloworld {
    public static void main(String[] args) {
        System.out.println("Input two numbers:");
        Scanner sc1 = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        number num = new number(sc1.nextInt(),sc2.nextInt());
        num.ShowNumber();
        num.Swap();
        num.ShowNumber();
        sc1.close();
        sc2.close();
}
}

That's two ways it

Perhaps in the future there will be a new method of adding pair of ~

That time will also continue to update it ~

 

Guess you like

Origin www.cnblogs.com/ever17/p/11278667.html