Java Basic methods

Source: Internet
Author: User

Definition and invocation of a method
method definition, method, function, in fact, refers to a specific function operation: The program to complete the independent function, reusable piece of code collection; method definition Format:    [Modifier]  Type method Name of the return value  ([formal parameter,....])    {             method body             [return  value];    } Method must be called in order to take effect (think of the story of marrying a daughter-in- law/ watching a menu order).   If the method uses the static adornment:       At this point we use the name of the class where the method resides. Method name (parameter);   If the method does not use the static adornment:      At this point we have to invoke the object of the class where the method is located (for the time being, object-oriented).

 Public Static void Main (string[] args) {      //TODO}main method is specifically called by the JVM, we just start the JVM.
location of the method definition    : 1): defined in the class, the smallest program unit in Java is the class.     2): Method definition In addition to other methods, methods and methods are fraternal relationships    . 3): The sequence of method definitions does not affect.
Methods of belonging to and how to define methods
method defines the format:  [Modifier]   Return value type   method Name ([parameter 1, Parameter 2,...])  {         method body;          [If the method needs to return a result to the caller, the return value is used at this time];  } The terms in the method:   modifiers:public, Static, and so on, static decorated methods belong to the class, are called directly with the class name, and now use the static adornment.   Return value type: The method actually completes a function, after the function is finished, whether to return a result to the caller.                           If you do not need to return the result to the caller, the keyword Void is used to declare it without the return meaning.   Method Name: Follow the specification of the identifier, using the verb, the first letter lowercase, if more than one word composition, using hump notation, every word in the future, the first letter is capitalized, Getemployeename.   Formal parameters: Methods The variables in parentheses, only the placeholder, the name of the parameter does not matter, the formal parameters can have   more than one. = = The type of the parameter + the number of parameters  + The order of the parameters.    = = Method name  +   method parameter list;                       In the same class, the method signature is unique, otherwise the compilation error.   Method Body: The code in the {} of the method that represents the code that specifically completes the function.   Return value: Inside the method, use the return keyword;                           Function 1: Returns a result value to the caller, at which point the method cannot use void decoration.                           Function 2: End the current method.   Actual parameter: The value of the parameter actually passed by the caller when invoking a specific method.    The caller of the method: Where to invoke a method, then where is the caller of the method.

How to define a method:1): What functional methods need to be defined. Don't be too tangled, follow me. 2): Whether the return value type needs to be defined. 3): Form parameters are required.-----------------------------------------------------------------------1): Whether you need to define a return value type: We say that the method is actually to complete a function, then the completion of the function, whether the caller needs to return a result data, if you do not need to return the result data, at this time using the Void declaration (no return).        If you need to return a result data, we will use the result data type as the return value type of the method.        Print operations: We care about the process of executing the method, not the result, so we use the Void declaration at this point. Ask for the sum of two numbers: We care about the process of executing the method, and after execution, we need to give the caller a feedback.2): Form parameters are required.       The method is in the process of completing this function, whether there are unknown factors involved, if invited to pass as a parameter, if there is no formal parameters. For the sum of two numbers, how many of these two numbers, in fact, is unknown to the method, just the caller knows, and different callers pass different parameter values.-----------------------------------------------------------------------pin for a method that has a return, the caller should define a variable to accept the returned result.-----------------------------------------------------------------------A: No parameter no return, B: Parameter has no return, C: No parameter has return, D: parameter has returned

The characteristics of the method-the execution process:

Method overloading Design

Requirement 1: In the same class, define the method of two integers, and the sum of decimals respectively.

Solution:

Requirement 2: In the same class, print the method of the String,int,double,boolean type separately.

Overloaded design of Methods (overload): Overloaded methods are defined in the same class, where a method allows more than one method of the same name, as long as their argument lists are different. Method overloading: Methods that mask the same functionality are different because of the different parameters that result in the method name. Method overload judgment principle: "Two identical" two same: in the same way, the method name is the same; a different: The method parameter list is different (parameter type, parameter number, parameter order);        as long as the parameter type, the number of parameters, the parameter order has a different, The parameter list is different. Note: Method overloads are independent of the return value type of the method, except that the return value type is generally required to be consistent.              the parameter list and the name of the parameter are OK, the overloads of the method and the formal parameters do not matter.

Then everyone thinks, why System.out.println () can print any type of data? View the source code to prove the overloaded relationship of multiple println methods.

The following methods are all in the same class value, and the method name is DoWork.voidDoWork (intACharBBooleanc) {} Three parameters: int type, char type, Boolean type. The following methods are the overloaded methods of the above DoWork methods. A.voidDoWork (CharBintABooleanc) {}//YES: Parameter types are differentB.intDoWork (BooleanACharCintb) {}//YES: Parameter types are differentC.voidDoWork (intACharBDoublec) {}//YES: Parameter types are differentD.voidDoWork (intXCharYBooleanz) {}//Compile error: not overloadedE.intDoWork (intXDoubleY) {}//YES: Different number of parametersF.intDoWork (intXCharYBooleanz) {}//Compile error: not overloaded
Understanding overloading the function of the overloaded definition overload the principle of judging overloading: two identical: The same as the method name in the same class: different      parameter list (parameter type, parameter order, number of parameters) (three in one, parameter class table is different) is not related to return value type
Method Recursive operation
recursion in the high school series, we have seen the Fibonacci sequence, the formula is: F (0) =0,f (1) =1,fn=f (n-1) +f (n-2) (n>=3,n∈n*), now ask F (5) value, how to do it? Observation: This sequence starts with the third item, each of which equals the sum of the first two. for a value of f (5), the value of f (4) and F (3) must be sought first, and the value of f (4) requires the values of F (3) and F (2). Workaround 1: The F(1), F (2), F (3), F (4) values are calculated in turn, and then processed. This method is very stupid, low efficiency is very low. The function of FN is to find the functions of NUM number, andthe function value needs num-1 and num-2 function values, and the two function values of the same method and to find NUM, then you can use the same approach.

JVM Memory model

JVM memory partitioning, artificially based on the storage characteristics of different memory spaces and stored data: Program counter: The line number indicator of the byte code executed by the current thread. Local method Stack: the native method service used for the virtual machine. Java Virtual Machine stack: Describes the memory model that the Java method executes, and when each method is executed, it creates a stack frame for storing local variable tables, operation stacks, dynamic links, method exits, and so on.     Each method creates a stack frame, and the stack frame holds the current method's data information (local variables), and when the method call is complete, the stack frame of the method is destroyed. Java heap: A chunk of memory that is shared by all threads and created when the virtual machine is started. All object instances and arrays are allocated on the heap (using the new keyword means a new storage space is opened in the heap). Method Area: A thread-shared memory area that stores class information loaded by a virtual machine, constants, static variables, code data compiled by the immediate compiler, and so on (the memory reclamation target for this area is primarily for the collection of constant pools and unloading of types).
GC (Garbage Collection): garbage collector. Java's automatic garbage collection mechanism: simply understood, programmers do not need to manually control the release of memory. When the JVM finds that memory resources are strained, it automatically cleans up memory space consumed by useless objects (objects that are not referenced). Array /object-oriented re-speaking.
Value passing mechanism of method parameters

value passing mechanism of method parameters: Transfer of value (Paint analysis) basic data type: Transfer of the copy of the argument value (stack) // Change Value Example: Define a method, change the value, and then only change the reference data type after it is called in main: transfer of the copy of the address value (stack, heap) // swaps the position of an element in two arrays. example: {10,99} becomes {99,10} first prints out the elements inside the defined array. (Iterate over an element in an array) and then define the method (referring to a third-party variable) to exchange the transfer mechanism of the last call method parameter

Java Basic methods

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.