Java syntax 2

Source: Internet
Author: User

I. Functions
1. 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 return value;
}

  1. Return Value Type: the data type of the result after the function is run.
  2. Parameter type: it is the data type of a formal parameter.
  3. Form parameter: a variable used to store the actual parameters passed to the function when a function is called.
  4. Actual parameter: the specific value passed to the form parameter.
  5. Return: Used to end a function.
  6. Return Value: this value is returned to the caller.

2. Features of functions

  1. Define a function to encapsulate the function code.
  2. This feature is easy to reuse.
  3. The function is executed only when it is called.
  4. The emergence of functions improves code reuse.
  5. If the function does not return a specific value, the return value type is critical.
  6. The Return Statement in the function is
  7. Rows can be omitted 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 and submitted to the caller.
.

3. function applications
 
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 incorrect.
. (Two parameters: int and INT)
• Code:
Int getsum (int x, int y)
{
Return X + Y;
}

4. Function Overloading
1. 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.

2. Features of heavy load:
It has nothing to do with the type of the returned value. It only looks at the parameter list.

3. Benefits of heavy load:
It facilitates reading and optimizes program design.

4. Example of heavy load:
// 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 ;}

Array 2,

1. 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 };

2. Memory Structure
When a Java program is running, it needs to allocate space in the memory. To improve computing efficiency, space is divided into different regions, because each region has a specific data processing and memory management mode.

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.

3. Array Operation FAQs
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.

3. 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;

Exercise: Obtain the sum of all elements in the ARR array. Use the nested loop of.
Note: int [] X, Y []; X indicates a one-dimensional array, and y indicates a two-dimensional array.
// Traverse two-dimensional arrays
For (INT x = 0; x <arr. length; X ++)
{
For (INT y = 0; y <arr [X]. length; y ++)
{
System. Out. Print (ARR [x] [Y] + ",");
}
System. Out. println ();
}

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.