The fifth day of Basic Java learning-methods and Arrays
| Document version |
Development Tools |
Test Platform |
Project name |
Date |
Author |
Remarks |
| V1.0 |
|
|
|
2016.02.24 |
Lutianfei |
None |
Chapter 3 Basic Java syntax Method
The method is to complete the code block of a specific function, that is, the function.
Format:
Modifier return value type method name (parameter type parameter name 1, parameter type parameter name 2 ...) {Function body; return value ;}
Explanation:
Modifier. Currently, public static is used. Return Value Type: the data type of the function result. Method Name: compliant with naming rules for convenient calling. Parameters:
Actual parameter: it is actually involved in the calculation. Form parameter: it is defined in the method and used to receive actual parameters. Parameter type: the data type of the parameter. Parameter Name: variable name method body statement: it is the code to complete the function return: End method. Return Value: The result of the function, which is returned to the caller.
Method overloadMethod overload Overview
In the same class, more than one
Method with the same name, As long as their
Number of parametersOr
Parameter typeDifferent.
Feature of method Overloading
And
The return value type is irrelevant., Read only
Method NameAnd
Parameter ListDuring the call, virtual machines use different parameter lists to differentiate methods with the same name.
Eg:
class FuncionTest{ public static void main(String[] args){ //Test1 short b1 =3; short b2 =4; System.out.println("short:"+compare(b1,b2)); }//*******************************************************//// Function :byte type public static boolean compare(byte a, byte b){ System.out.println("byte type"); return a==b; }//-------------------------------------------------------////*******************************************************//// Function :short type public static boolean compare(short a, short b){ System.out.println("short type"); return a==b; }//-------------------------------------------------------////*******************************************************//// Function :int type public static boolean compare(int a, int b){ System.out.println("int type"); return a==b; }//-------------------------------------------------------////*******************************************************//// Function :long type public static boolean compare(long a, long b){ System.out.println("long type"); return a==b; }//-------------------------------------------------------//
ArrayArray Concept
An array is a collection of multiple elements of the same data type. It can also be seen as a container. Arrays can store both basic data types and reference data types.
Array Definition Format
Format 1: Data Type [] array name; Format 2: Data Type array name []; Note: after the two definitions are complete, there is no element value in the array. You also need to initialize the elements of the array.
Eg:
A: int [] a; defines an int type array a variable B: int a []; defines an int type a array variable
Note:
1: two formats are defined for arrays. The first format is recommended. Because the first type is more readable. In the early stage, many people did use this. However, there are fewer and fewer people using this method. 2: C #, a Java fan, no longer supports the second syntax format. More and more languages may discard the second format.
Array initialization Overview:
The array in Java must be
Initialization before using. Initialization: it is an array element in the array.
Allocate memory spaceAnd for each array element
Assignment.
Array Initialization Method
Dynamic Initialization: Only the array is specified during initialization.
Length,
SystemAllocate an array
Initial Value.
Static Initialization: Specify
Initial Value,
SystemDetermines the Array
Length(More ).
Dynamic initialization: only the length of the array is specified during initialization. The system assigns an initial value to the array.
Format:
Data Type [] array name = new data type [array length];
Array LengthIt is actually in the array
Number of Elements. Example:
Int [] arr = new int [3]; Explanation: defines an array of the int type, which can store three int type values.
newAllocate memory space to the array.
Static initialization: Specify the initial values of each array element during initialization. The length of the array is determined by the system.
Format:
Data Type [] array name = new data type [] {element 1, element 2 ,...}; Example:
Int [] arr = new int [] {1, 2, 3 };Explanation: an array of int type is defined. This array can store three int type values, which are 1, 2, and 3. Simplified writing
Int [] arr = {1, 2, 3}; Note: Dynamic and Static initialization cannot be performed simultaneously. That is, the element cannot be given while the length of the array is given.
Memory Allocation in JavaWhen a Java program is running, it needs to allocate space in the memory. To improve the computing efficiency, we divide the space into different regions, because each region has a specific data processing and memory management mode.
StackStorage
Local variableAnd disappears after use.
HeapStorage
Something new Method Area(Object-Oriented)
Local Method Area(Related to the system)
Register(For CPU usage)
Local variable
In
Defining MethodsOr
Method DeclarationAnd disappear immediately.
Heap: New, entity, and object.
A: Every entity has
First address valueB: data in each object has
Default Value
Byte, short, int, long: 0 float, double: 0.0 char: '\ u0000' boolean: false reference type: null c: The Garbage Collector
When idleRecycle.
Graphic storage space
Graphic Array Memory three Arrays
/* Requirement: 1. Define the first array. assign a value to the array element after the definition is completed. After the value is assigned, the name and element of the output array are displayed. 2. Define the second array. After the definition is complete, assign values to the array elements. After the value is assigned, the name and element of the output array are displayed. 3. Define the third array and give it the address value of the first array. (Note that the types are consistent), and the element is assigned a value through the name of the third array. 4. output the name and element of the first array again. */Class ArrayDemo {public static void main (String [] args) {int [] arr = new int [3]; arr [0] = 88; arr [1] = 33; arr [2] = 66; System. out. println (arr); System. out. println (arr [0]); System. out. println (arr [1]); System. out. println (arr [2]); int [] arr2 = new int [3]; arr2 [0] = 22; arr2 [1] = 44; arr2 [2] = 55; system. out. println (arr2); System. out. println (arr2 [0]); System. out. println (arr2 [1]); System. out. println (arr2 [2]); // The third array int [] arr3 = arr; arr3 [0] = 100; arr3 [1] = 200; System. out. println (arr); System. out. println (arr [0]); System. out. println (arr [1]); System. out. println (arr [2]);}
Two common problems in Array OperationsArray Index out-of-bounds exception
ArrayIndexOutOfBoundsException cause: an index that does not exist in the array is accessed.
Null Pointer exception
NullPointerException cause: When the array reference does not point to an object, but the elements in the object are operated (the array is no longer in the stack memory ).
Eg:
Int [] arr = {1, 2, 3}; System. out. println (arr [3]); // error: ArrayIndexOutOfBoundsExceptionarr = null; System. out. println (arr [0]); // error: NullPointerException
Array exercisesArray traversal (output each element in the array in sequence)
Array attributes
lengthTo get the length of the array.
Int [] arr2 = {,}; // length attribute for (int x = 0; x
Obtain the maximum and minimum values of an array)
/* Analysis: A: Find A reference object from the array (usually the first one), which is the largest by default. B: traverse other elements and compare them with me. */Class ArrayDemo {public static void main (String [] args) {int [] arr2 = {66,323, 8,654, 2}; int max = arr2 [0]; int min = arr2 [0]; for (int x = 1; x arr2 [x])? Max: arr2 [x]; min = (min <arr2 [x])? Min: arr2 [x]; // System. out. println (arr2 [x]);} System. out. println ("Max is:" + max); System. out. println ("Min is:" + min );}}
Array elements are in reverse order (that is, elements are reversed)
/* Analysis: A: swap arr [0] With arr [length-1] B: swap arr [1] With arr [length-2... c: As long as arr is enabled. length/2: */class ArrayDemo {public static void main (String [] args) {int [] arr = {66,323, 8,654, 2}; for (int I = 0; I
Array Lookup (index input based on the keyboard to find the corresponding week)
Import java. util. role; class ArrayDemo {public static void main (String [] args) {String [] strArr = {"Monday", "Tuesday", "Wednesday", "Thursday ", "Friday", "Saturday", "Sunday"}; latest SC = new week (System. in); System. out. println ("Please input a number (0 ~ 6): "); int index = SC. nextInt (); System. out. println (strArr [index]);}