One, array
1. The concept and function of arrays
An array is a collection of elements of the same data type;
The array itself is a reference data type, that is, an object. But arrays can store basic data types, or they can store reference data types.
For example:
int [] A = new int [] {1,2,3,4,5,6,};
string [] s = new string [] {"Rabbit", "Little Bunny", "Little Bunny",};
Employee [] e = employee [ten];(employee is a custom class).
2, the declaration of the array
There are two ways to declare an array:
data type [] array name for example: int [] A;
Data type array name [] For example: int a [];
Note: There is no difference between the two methods of declaring in the Java language, but it is recommended that you use the first one to avoid confusing the data type of a.
The array is only a null pointer after the declaration and cannot be used, and must be created to use.
3. Creation of arrays
There are three ways to create an array:
1)//Declare the array while allocating memory according to the length of the set, but the element values in the array are the default initialization values
char [] chary = new char [10];
2)//Declare the array and allocate memory, and initialize it
int [] ary1 = new int [] {1,2,3,4,5};
3)//In the same way as in the previous one, just a relatively brief syntax
int [] Ary2 = {1,2,3,4,5};
From another perspective, array creation can be divided into two types, dynamic and static
1) dynamically create an array (no value assigned to the element, can be assigned with a For loop)
char [] chary = new char [10];
2) Create an array statically, assigning an initial value to each element when it is created
int [] ary1 = new int [] {1,2,3,4,5};
Length of array: Long property
int [] B1 =new int [] {1,2,3,4,5,6,7};
System.out.println (b1.length);
Note: The length of the array is an attribute, and the length of the string is long ();
The array length must be specified when the array is created and is not allowed to change once defined.
Although the array length is 7, it actually gives 8 locations in memory, and the other stores 7.
assigning values between arrays
In Java, the value of an array can be syntactically assigned to another array, but the actual effect is that two array references point to the same piece of memory.
int [] Ary1 = {1,2,3,4,5,};
int [] Ary2;
Ary2 = Ary1:
The reference to the above two arrays points to the same piece of memory.
public class Arraydemo {
public static void Main (string[] args) {
Int[] Ary1 = {2, 4, 6, 8, 10}; //Declare and initialize an array of 1
Int[] Ary2; Declares an array of 2 Ary2 = Ary1; //Assign array 1 to array 2
ARY2[3] = 1024; //Modify the value of one of the elements through array 2
Prints the element System.out.println ("element in array 1:") in Group 1;
for (int i = 0; i < ary1.length; i++) {
System.out.println (Ary1[i]);
}
Prints the element System.out.println ("element in array 2:") in Group 2;
for (int i = 0; i < ary2.length; i++) {
System.out.println (Ary2[i]);
}
}
}
Run Result: element in array 1 2 4 6 for loop output is 1024
Element in array 2 2 4 6 for loop output is 1024
Second, operator
By function, operators in Java can be divided into four classes
Arithmetic operators
Bitwise operators
Comparison operators
logical operators
besides = = and! =, +, other operators are operations on basic data types, = = and! = can perform operations on basic data types and reference types, + can also connect strings.
1. Arithmetic operators
Monocular: + (Take positive)-(minus) + + (self-increment 1)--(self minus 1)
Binocular: +-*/% (remainder) (+ can also connect string )
Trinocular: a > B? Ture:false
Note: When a is greater than B, it is true (that is, the value before the colon), otherwise false; The entire operator includes a relational operator (can be ">" "<"! = "and so on), a"? ", a": ", you need to have two expressions or a value or an object before and after a colon.
2, bitwise operators
Bitwise operators: (Convert to binary before operation )
With (&), non-(~), or (|), XOR (^)
& All is 1, the result of the operation is 1, the others are 0;
~ Take the Counter +1, add 1 after the reverse (take negative);
| As long as one is 1, the result is 1;
^ The same result is 0, the different result is 1;
Shift operator:<<, >> (signed right shift)
Shift left 1 is the equivalent of multiplying by 2, right shift 1 is equivalent to dividing by 2, more efficient than multiplication operation
For example:
int a = 8, C;
c = a >> 2;
Original: (binary value of 8 is) 0000 1000
After move: (the value of C is 2) 0000 0010
3. Comparison operators
= = (equals sign),< (less than),> (greater than), <= (less than equals), >= (greater than equal sign),! = (not equal);
= = vs. = Compare the base data type is the comparison value, and the comparison reference type is the virtual address;
In addition to = = = =, only the basic data types can be compared;
Note: The return value of the comparison operator is ture or false.
4. Logical operators
Expressions for connecting two Boolean types
&& (double and), | | (dual or), & (with), | or), ^ (XOR),! (non-operational)
Precautions:
1) && and & operation results are the same, for & regardless of the left-hand why value, the right side are involved in the operation;
For &&, as long as the left is false to the right, it is no longer calculated and returns false directly.
2) | | The result of the operation is the same as that for | The right side, regardless of the value of the left, participates in the operation;
for | | , as long as the left is true, the right side is no longer calculated and returns true directly.
3) && | | Can be understood as a physical short-circuit, the actual multi-use && and | |.
Third, the process control in Java
Sequential process
Branching process
Cycle flow
1. Sequential process
Input-----Operation processing-----Output
2. Branch process
1) Conditional statement if ....
A conditional expression can be any type of logical expression;
If the expression value is true, the contents of the curly brace are executed, and then the following statements are executed;
If the expression value is false, the following statement is executed directly;
If the curly braces have only one statement, you can omit the curly braces (recommended not omitted);
2) Conditional statement if ... else .....
Syntax Format-if.....case ....
if (expression) {
Executing a statement block 1
}else{
Executing a statement block 2
}
If the expression value is false, then the statement block of else is executed;
3) Conditional Statement-switch....case (casevalue is a specific value, cannot have duplicates, is constant )
Syntax format
switch (expression) {
Case value 1: statement block 1
....
Case Value N: statement block n
Default: statement block n+1
}
The value of an expression can only accept int, Byte, char, short, enumeration, JDK1.6 string can also, do not accept other types of values, do not allow duplicate case value;
When switch encounters the first case match, the program jumps to the label position;
Start the execution of all subsequent program code, regardless of whether the following case conditions match, until the break statement is encountered;
3. Circulation process
1) Loop statement-while ( loop body in Whlie )
while (conditional expression) {
Executes the statement block;
}
2) Loop statement-do...while at least one loop ( loop body in do)
do{
Executes the statement block;
}whlie (conditional expression);
3) Circular Statement-for
for (initialize expression; loop condition expression ; post-loop action expression) {
Executes the statement block;
}
4. Loop Interrupt Statement
Interrupt statement
Labels can be used in multiple loops to specify the specific loop of interrupts
When you use a loop statement, you can end the loop only if the value of the loop condition expression is false. Sometimes we want to interrupt the loop prematurely, to do this, simply add a break or continue statement to the LOOP statement block
1) Interrupt Statement-break
The break statement is used to terminate execution of a block of statements. Used in the Loop statement body, you can forcibly exit the loop (the entire loop).
"Break;" Statement: can appear in the while, Do...while, for, switch statement body.
Break Label statement: can appear in any body of the statement. The majority is still used in the loop.
2) Interrupt Statement-continue
The continue statement is used in the Loop statement body to terminate a looping process, skipping a loop that is not executed under the Continue statement in the loop body (out of this loop) and starting the next cycle.
"CONTINUE;" Statement: can only appear in the Loop statement while, Do...while, for.
Some understandings of learning arrays, operators, and Process control in the Java language