Parameters passed in java

  Java is only a call-by (passed by value), no pass-by-call (or site transfer passed by reference). Therefore, changes in the value of java method parameters will not change the value of the original variables, but why change the property value reference variable but can it? Consider the following answers.

  java Data Type

  In Java data types are divided into two categories: primitive types and reference types. Accordingly, these variables can be divided into two types: basic types and reference types.

  The basic types of variables stored original value, that value is the value that represents itself;

  The preservation of reference type variable value is a reference value, the "reference value" points to the memory address space, represents a reference to an object, not the object itself, the object itself is stored in the address location indicated by the reference value.

  The basic types: byte, short, int, long, char, float, double, Boolean, returnAddress.

  Reference types include: class, interface types and arrays.

  java value transfer only

  In everyday coding, you will often see the phenomenon as follows:

  1, the basic types of parameters, the parameters reassigned vivo, not change the value of the original variables.

  2, a reference to the type of parameters, the parameters given in reference to re-vivo, does not change the original variables held by reference.

  3. The method of calculating the parameters in vivo, not change the value of the original variables.

  4, a reference to the type of parameters, the method of operating parameters vivo pointing object attributes, the attribute values ​​change the original variable pointing object.

  for example:

  public class Main {

  private static void getMiddleOne(boolean b, Boolean boo, Boolean[] arr){

  b = true;

  boo = new Boolean(true);

  arr[0] = true;

  }

  //test

  public static void main(String[] args) {

  boolean b = false;

  Boolean boo = new Boolean(false);

  Boolean[] arr = new Boolean[]{false};

  getMiddleOne(b, boo, arr);

  System.out.println(b);

  System.out.println(boo.toString());

  System.out.println(arr[0]);

  /**

  * output:

  * false

  * false

  * true

  */

  }

  } Wuxi gynecological hospital Which http://www.wxbhnkyy39.com/

  As long as we understand the following two points can answer the above phenomenon:

  1, the value of the basic data type is the value itself, so the value of b in the example is false; packaging because boxing and unboxing automatically, and can be substantially the same as the type of treatment, the value sample boo is false; arrays are reference type, the value is directed to the Boolean arr [] reference.

  2, java only passed by reference is not passed by value, so that the three parameters are passed getMiddleOne method copies the value of b, the value of Boo copy, copy the value of ARR.

  Through the above two points will be clear, b = true and boo getMiddleOne process executed = new Boolean (true) is assigning a new value of their copies, without changing the value of the original variable; Similarly, arr [0 ] = true is true copy to the first copy of arr element of the array pointed to by the value and the value of copy arr arr the array is referenced, it copies the array pointed arr arr pointed and the array is the same, so changing the elements of the array arr copies will also affect the original variable arr.

  to sum up

  java value transfer only the basic type copy of the value is passed, a reference type of transmission is a reference copy.

Guess you like

Origin www.cnblogs.com/djw12333/p/11309235.html