Getting Started with Java (3)--functions

Source: Internet
Author: User

Functions and methods

This process can be encapsulated as a function if we often have to perform similar processes.

Functions can be called repeatedly, allowing for code reuse and isolation purposes.

In object-oriented languages, functions are often bound together with objects, and for the sake of distinction, it is called a method .

Because Java is completely object-oriented, the function must belong to a class. So the functions in Java are called methods.

If the method is statically decorated before, it is called a static method , and it can be roughly assumed that it is roughly equal to the concept of a C-language function.

Method can list the parameters that need to be prepared when calling it in parentheses, called formal arguments .

The method can also have a return value (or no, write void at this time).

Next, we call method F to find out if the two integers are the same.

1  Public classA04032 {3     Static BooleanFintAintb) {4         intA1 = a% 10;5         intB1 = b% 10;6         returna1==B1;7     }8     9      Public Static voidMain (string[] args) {TenSystem.out.println (f (101,12)); OneSystem.out.println (f (65432,12));  A     } -}

This method takes two parameters, all integers, and returns a Boolean value after a series of processing.

The return statement ends the execution of the F method and returns a specified value.

After the return statement is encountered, the other code of the F method is no longer executed, but is returned to the method that called it. Of course, in this method, there is no more statements after the return statement.

If a method defines a return value, it must encounter a return statement before it finishes execution, or a compilation error is thrown.

Independent principle of formal parameters

If the value of the formal parameter is changed in the modulated function, will it affect the calling party? No!

Let's look at this example:

1  Public classA04042 {3     Static voidFintx) {4         intsum = x% 10;5X/= 10;6sum = sum * ten + x% 10;7X/= 10;8sum = sum * ten + x% 10;9 System.out.println (sum);TenSystem.out.println ("x=" +x); One     } A      -      Public Static voidMain (string[] args) { -         intA = 368; the f (a); -System.out.println ("a=" +a);  -     } -}

The function f here is to invert and output the digits of the 3-digit x passed to it.

For observation, we output an extra value of a and the value of X.

As you can see from the results, although the value of X has changed during the calculation, this does not affect the value of a.

In fact, before calling F, to prepare the required parameters for it, these parameters must be newly created, so they are called formal parameters.

That is, these parameter variables do not exist when the F function is not called, and these parameters are created multiple times when F is called multiple times.

After the parameter is created, the value of the argument (this is the value of a) is copied to it before the execution of F is started.

When the F executes, the shape parametric is automatically released.

From the lower mechanism, the allocation and release of this parameter is done through the stack.

Before the function call, to put the return position, the required parameters and other information stack, after the function is completed, the automatic stack, restore the appearance before the execution.

If the called function also calls other functions, the process will continue to perform.

This way, the stack may rise higher, but the execution of the function always ends when the stack falls back.

It is common for a program error to cause the stack to overflow if the function call does not return correctly due to a design error, but instead calls the other function repeatedly.

From the principle of parameter passing, we can see that the real participation parameters are independent variables, except that the arguments are copied to the formal parameters in the beginning, and they will not have any relation with each other. It's called

" formal parameter independent principle ".

This design saves us a lot of trouble, but sometimes we might want to let the callee share a variable with the keynote party instead of being independent.

The answer is to pass a pointer in Java called: Reference.

Reference as parameter

The nature of the reference is to hold the address of another object.

If a reference is copied, only two references point to the same object, and the object itself is not copied.

References are closely linked to object-oriented systems, so we can understand them better when we learn the initial object-oriented knowledge.

But here, we can first look at the array's behavior to see its clues.

An array is an object type, and we define an array variable that is actually a pointer to an actual array object, or a reference.

1  Public classA04042 {3     Static voidFint[] x) {4          for(inti=0; i<x.length; i++){5             if(X[i] < 0) X[i] =-X[i];6         }7     }8     9      Public Static voidMain (string[] args) {Ten         int[] A = {5,-3, 6, 10, 0,-15}; One f (a); A System.out.println (Java.util.Arrays.toString (a)); -     } -}

Here, parameter x, argument A are not the arrays themselves, they are references to arrays.

Although X and a also follow the same rules, they do not affect each other, but because they point to the same object, this causes complex phenomena.

From the result, we can observe that the value of the array object is modified in the F method, and these changes are seen when the keynote side prints the array.

Its schematic diagram:

Reference is a common means of sharing data between the keynote function and the tuned function

Getting Started with Java (3)--functions

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.