Java method parameters-think about interesting issues, java is quite interesting

Source: Internet
Author: User

Java method parameters-think about interesting issues, java is quite interesting

It has always been considered that all method parameters in Java are passed values and will not affect itself after the call.

Java does not have a pointer in C/C ++. In the fast sorting, the input array changes the value. Thoughts:

// For the convenience of examples, the following is a quick sorting of pseudo code

Input array. In recursion, the array value is operated.

void quickSort(int s[], int l, int r){      if (l < r){         quickSort(s, l, i - 1);         quickSort(s, i + 1, r);      }  }  

 

Conclusion: The value is passed in Java method parameters. When the parameter is of reference type (such as an array), the value of the memory address of the array is passed in and can be operated on.

 

Principle:

 

The Mechanism in Java is as follows:

 

(--- Explanation in Java core technology-Volume 1)

 

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

The reference type points to an object, not the original value. The variable that points to the object refers to the referenced variable.

(Similar to pointers in C/C ++, pointing to object entities (specific values) in a special way to store a memory address)

Note: String, array, interface, and class are all reference types.

Array discussion:

Int [] a = new int [3]; double [] B = new double [3];

String [] s = new String [3]; example [] e = new example [3];

No matter which type of Array (), the array identifier (a, B, s, e) is actually a reference, pointing to a real object created in the heap.

The object array (s, e) and basic type array (a, B) are used in the same, unique difference (the value saved in the array) the object array stores the reference, and the basic type array directly saves the basic type value.

(Refer to Java programming Ideas Section 16.2 why are arrays special)

 

References in Java are equivalent to a restricted pointer. A reference is obtained when a new object is created.

For example, String ex = new String ("example ");

Therefore, in the recursive operation of quick sorting, the address value of the array is input. In each recursion, operations on the array will actually change the value of the array.

// It is not easy to learn. If it is inappropriate, I hope I can correct it.

 

Reference: <Java core technology-Volume 1> Chapter 4.2 method parameters

Advanced Reading: <Java programming ideology> Chapter 4 Why are arrays special?

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.