function, memory, one-dimensional array, two-dimensional array "4"

Source: Internet
Author: User
Tags array length

Definition of a function

What is a function?

• A function is a separate applet defined in a class that has a specific function. • Functions are also called methods. The format of the function :• modifier Returns a value type function name (parameter type form parameter 1, parameter type parameter 2, ...)

{

Execute the statement;

return value;

}

Return value type: The data type of the result after the function is run.

Parameter type: is the data type of the formal parameter.

Formal parameter: is a variable that stores the actual arguments passed to the function when the function is called.

Actual parameter: The specific value passed to the formal parameter.

return: Used to end the function.

Return value: The result of the function's operation, which is returned to the caller.

Features of the function

define functions to encapsulate the function code

facilitates reuse of this feature

function is executed only if it is called

The appearance of functions improves the reusability of code

for cases where the function does not have a specific return value, the return value type is represented by the keyword void , then the return statement in the function can be omitted if the last line is not written.

Note:

• Only functions can be called, and functions cannot be defined inside a function.

• When defining a function, the result of the function should be returned to the caller and referred to the caller for processing.

Application of the function

two Clear• What is the final result of defining the function to be defined? • Specify whether unknown content is required to participate in the operation when defining the functionExample:• Requirements: Define a function that can implement addition operations for two integers. • Analysis:• What is the result of this function? Two number of and, also an integer (int)• Are there unknown content involved in the process of implementing this function? Addend and Summand are uncertain. (two parameter int,int)• Code:

int getsum (int x,int y)

{

return x+y;

}

overloading of functions (overload)

The concept of overloading

In the same class, more than one function with the same name is allowed, as long as the number of arguments or the type of the parameter are different.

Features of overloading:

Regardless of the return value type, see only the parameter list.

Benefits of Overloading:

Easy to read and optimize the program design.

overloading Example:

Returns the number of two integers and

int add (int x,int y) {return x+y;}

Returns the number of three integers and

int add (int x,int y,int z) {return x+y+z;}

Returns two decimals and

Double Add (double x,double y) {return x+y;}

function, just as the unknown content of the participating operation is not the same,

You can define multiple functions, but use the uniform function name so that it is easy to read.

When called, the virtual machine distinguishes a function with the same name by different argument lists.

Array

definition of an array

Concept

A collection of the same type of data. The array is actually a container.

The benefits of arrays

The elements in the array can be automatically numbered starting with 0, allowing them to be manipulated.

Format 1:

element type [] Array name = new element type [element number or array length];

Example: int[] arr = new INT[5];

Format 2:

element type [] Array name = new element type []{element, element, ...};

int[] arr = new int[]{3,5,1,7};

Int[] arr = {3,5,1,7};

Array memory structure

Memory structure

Java programs need to allocate space in memory at run time. In order to improve the efficiency of operation, space is divided into different regions, because

There is a specific way of handling data and memory management for each piece of area.

Stack memory

For storing local variables, the occupied space is automatically freed when the data is used.

Heap Memory

arrays and objects are stored in heap memory by the instances created by new. Each entity has a memory address value The variable in the entity has a default initialization value entity is not being used and will be reclaimed by the garbage collector for an indeterminate amount of time

Method area, local method area, register

Array Operations FAQ

Array pin out of bounds exception (arrayindexoutofboundsexception)

int[] arr = new int[2];

System.out.println (Arr[3]);

Occurs when a non-existent script in the array is accessed.

Null pointer exception (NULLPOINTEREXCEPTION)

int[] arr = null;

System.out.println (Arr[0]);

The ARR reference does not point to an entity, but when an element in an entity is manipulated.

Two-dimensional array []

Format 1:int[][] arr = new int[3][2];

defines a two-dimensional array called arr 3 One-dimensional arrays in a two-dimensional array 2 elements in each one-dimensional array the names of one-dimensional arrays are arr[0], arr[1], arr[2] Assigning a value of 78 to the first one-dimensional array of 1 pin-marks is: arr[0][1] = +;

Format 2:int[][] arr = new int[3][];

3 One-dimensional arrays in a two-dimensional array each one-dimensional array is the default initialization value of NULL You can initialize this three one-dimensional array individually

Arr[0] = new INT[3];

ARR[1] = new INT[1];

ARR[2] = new INT[2];

Format 3:int[][] arr = {{3,8,2},{2,7},{9,0,1,6}};

defines a two-dimensional array called arr three one-dimensional arrays in a two-dimensional array the specific elements in each one-dimensional array are also initialized first one-dimensional array arr[0] = {3,8,2}; second one-dimensional array arr[1] = {2,7}; third one-dimensional array arr[2] = {9,0,1,6}; the length representation of the third one-dimensional array: arr[2].length;

Note Special wording: int[] x,y[]; X is a one-dimensional array, and Y is a two-dimensional array.

function, memory, one-dimensional array, two-dimensional array "4"

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.