Functions, memory, one-dimensional array, two-dimensional array [4], two-dimensional array of dimensions

Source: Internet
Author: User
Tags array definition

Functions, memory, one-dimensional array, two-dimensional array [4], two-dimensional array of dimensions

Author: pipi-changing

Original Source: http://www.cnblogs.com/pipi-changing/

    

The copyright of this article is shared by the author and the blog Park. This statement must be kept without the author's consent, and the original article is clearly connected on the article page. Otherwise, the legal liability will be held.

 

 

Function Definition

 

 What is a function?

• A function is an independent applet defined in a class with specific functions. • Functions are also called methods. Function Format: • Modifier Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 2 ,...)

{

Execute the statement;

Return value;

}

 

Return Value Type: the data type of the result after the function is run.

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

Form parameter: a variable used to store the actual parameters passed to the function when a function is called.

Actual parameter: the specific value passed to the form parameter.

Return: Used to end a function.

Return Value: The result of the function operation. The result is returned to the caller.

 

 Features of functions

 

Define a function to encapsulate the function code.

 

This feature is easy to reuse.

 

The function is executed only when it is called.

 

The emergence of functions improves code reuse.

 

If the function does not return a specific value, the return value type is expressed by the keyword void, so the return statement in the function can be omitted in the last line without writing.

 

Note:

 

• Functions can only be called and cannot be defined within the function.

 

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

 

 

 Function Application

 

Two definitions• What is the final result of the function to be defined? • Determine whether unknown content is required for calculation when defining this function Example: • Requirement: define a function to implement Addition of two integers. • Analysis: • What is the result of this function? The sum of two numbers is also an integer (int) • Is there any unknown content involved in the operation when this function is implemented? The addend and addend are uncertain. (Two parameters: int and int) • Code:

Int getSum (int x, int y)

{

Return x + y;

}

 

 Function overload)

 

Concept of heavy load

In the same class, more than one function with the same name is allowed, as long as they have different numbers of parameters or parameter types.

Features of heavy load:

It has nothing to do with the type of the returned value. It only looks at the parameter list.

Benefits of heavy load:

It facilitates reading and optimizes program design.

Overload example:

// Returns the sum of two integers

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

// Returns the sum of three integers.

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

// Returns the sum of two decimal places

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

 

Functions have the same functions, except that the unknown content involved in the operation is different,

You can define multiple functions, but use a uniform function name for easy reading.

During the call, the virtual machine uses different parameter lists to differentiate functions with the same name.

 

 Array

 

Array Definition

Concept

A collection of the same type of data. In fact, arrays are a container.

Array benefits

The elements in the array are automatically numbered from 0 to facilitate operations on these elements.

Format 1:

Element type [] array name = new element type [number of elements 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

 

When a Java program is running, it needs to allocate space in the memory. To improve the computing efficiency, we have divided the space into different regions.

Specific data processing and memory management methods are available for each region.

Stack memory

Used to store local variables. when data is used up, the occupied space is automatically released.

Heap memory

Arrays and objects are stored in the heap memory through the new instance. Each entity has a memory address value. All variables in the object have default initialization values. If the object is not used, it will be recycled by the Garbage Collector within an uncertain period.

Method area, local method area, register

 

 

Array Operation FAQ

 

Array footer out-of-bounds exception (ArrayIndexOutOfBoundsException)

Int [] arr = new int [2];

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

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

Null Pointer exception (NullPointerException)

Int [] arr = null;

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

When arr references elements in an object that do not point to the object.

 

 

Two-dimensional array [] []

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

Defines a two-dimensional array named arr. The two-dimensional array has three one-dimensional arrays. Each one-dimensional array has two elements. The names of one-dimensional arrays are arr [0], arr [1], and arr [2]. Assign a value to the 1st position of the first one-dimensional array to 78. The syntax is: arr [0] [1] = 78;

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

The two-dimensional array has three one-dimensional arrays. Each one-dimensional array is initialized with the default value null. The three one-dimensional arrays can be initialized separately.

Arr [0] = new int [3];

Arr [1] = new int [1];

Arr [2] = new int [2];

 

Format 3: int [] [] arr = {, 2}, {}, {9, 0 }};

Defines a two-dimensional array named arr. The two-dimensional array contains three one-dimensional arrays. The specific elements in each one-dimensional array have also been initialized. The first one-dimensional array arr [0] = {3, 8, 2 }; The second one-dimensional array arr [1] = {2, 7 }; The third one-dimensional array arr [2] = {9, 0, 1, 6 }; Length representation of the third one-dimensional array: arr [2]. length;

 

Note: int [] x, y []; x indicates a one-dimensional array, and y indicates a two-dimensional array.

 

 

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.