Java method Parameters-think of something interesting

Source: Internet
Author: User

Always think that Java 's method parameters are passed values, after the call does not affect itself.

Java does not have a pointer in C + + , in the fast sort, the incoming array, but the value of the change occurred. This leads to thinking:

For the convenience of example, the following is a partial quick sort pseudo-code

In the recursive array, the array values are manipulated.

void quickSort (intintint  r) {      if (L < r) {         -1);          + 1, R);      }  }  

Conclusion: A value is passed in theJava method parameter, in which the parameter is a reference type (such as an array), and the value of the memory address of the arrays is passed in, which can be manipulated.

Principle:

the mechanisms in Java are as follows:

( explained in ---Java core Technology • Volume 1 )

Basic data types (byte,int,char,long,float, Double,boolean , and short)

The reference type (reference type) points to an object, not the original value, and the variable that points to the object is a reference variable.

(like a pointer in C + +, a special way to point to an object entity (a specific value), to store a memory address)

Note:String , Array, interface, class are reference types.

Discussion of arrays:

Int[] A=new int[3]; Double[] B=new double[3];

String[] S=new string[3]; Example[] E=new example[3];

Regardless of the type of array (), the array identifier (a,b,s,e) is actually just a reference to a real object created in the heap.

      Object array (s, e) is the same on use, the only difference ( value saved in array )

( refer to Java programming idea 16.2 section array why special )

The reference in Java is equivalent to a restricted pointer, and a reference is obtained when new creates the object.

such as String Ex=new string ("example");

Therefore, in the recursive operation of the fast sort, the address value of the array is passed in, and the array is manipulated in each recursion, and the values of the arrays are actually changed.

Caishuxueqian, improper place, hope to correct.

Reference: <java Core Technology • Volume 1> Chapter 4.2 Method Parameters

Advanced reading:<java programming ideas > Why is an array special

Java method Parameters-think of something interesting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.