Syntax basics of the Java tutorial (bottom)

Source: Internet
Author: User

methods: Some places also called functions, in order to improve the reusability of the code, the content can be defined as a separate function, the embodiment of this function is the method in Java.

Java inside method defines the format:

modifier Returns a value type method name (parameter type form parameter 1, parameter type form parameter 1, ...) {

Execute the statement;

If you do not have a type that defines the return value, you do not have to return any content, otherwise:

return return value;

}

When the method does not have a specific return value, the returned value type is represented by the void keyword.

If the return value type of the method is void, you do not need to write the return statement.

return the function of: End method. End Function. Returns the result of the operation to the caller of the current method.

How do you define a method?

method is actually a function, the definition of the method is to implement the function (this block if you do not quite understand the words can find some Java video tutorial followed by learning), through two clear to complete:

1), clear the function of the results of the operation, to achieve what kind of arithmetic function. In fact, the return value type of this method is clarified.

2), in the process of implementing this function whether there is unknown content involved in the operation, in fact, is in the clear parameter list of this method (parameter type & parameter number).

How does the method work:

1), for defining functions.

2), used to encapsulate code to improve the reusability of code.

Note: Methods can only be called methods, and methods cannot be defined, when a method calls itself by itself called "recursion".

Main method:

1), to ensure the independent operation of the class.

2), because it is the entrance to the program.

3), because it is called by the JVM.

How does the method define the name?

A: 1), in order to mark the function, it is convenient to call.

2), in order to use the name to clarify the function of the method, the name of the method can best reflect the role of the method, in order to increase the readability of the code.

The following argument exists in the method:

overloading is defined as: In a class, if two or more than two methods of the same name appear, as long as they have the number of arguments, or the type of the parameter is different, it can be called the method overload.

How to differentiate overloads: when the method has the same name, only the argument list is viewed. And the return value type is okay.

7 , array: a container for storing data of the same type, note the same type of data. Benefit: You can number the data in the container, starting with 0. Arrays are used to encapsulate data, which is a specific entity.

How do you define an array in Java? Two definitions of the form.

1), element type [] Variable name = new element type [number of elements];

2), element type [] Variable name = {element 1, element 2 ...} ;

element type [] Variable name = new element type []{element 1, element 2 ...} ;

---------------------------------------------------------

in the array Two-point lookup method. There must be a prerequisite: the elements in the array are ordered:

public static int halfseach_2 (int[] arr,int key) {

int min,max,mid;

min = 0;

max = arr.length-1;

Mid = (max+min) >>1; //(Max+min)/2;

while (Arr[mid]!=key) {

if (Key>arr[mid]) {

Min = mid + 1;

}

else if (Key<arr[mid])

max = mid-1;

if (max<min)

return-1;

Mid = (max+min) >>1;

}

return mid;

}

---------------------------------------------------------

Java It is divided into the following several memory.

1: register. 2: Local method area. 3: method area. 4: stack. 5: heap.

Stacks: stores are local variables (variables defined in the method, parameters on methods, variables in statements);

The data is freed as long as the area where the data operation is completed ends.

heap: used to store methods and objects, that is, entities (JavaBean). Entity classes are used to encapsulate multiple data.

1: Each entity has a memory header address value.

2: variables in heap memory have default initialization values. Because the data types are different, the values are not the same.

3: garbage collection mechanism. There is an automatic garbage collection mechanism in Java that programmers don't need to be concerned about.

Syntax basics of the Java tutorial (bottom)

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.