I. Program writing (the initialization process for creating objects)
1. Purpose /Result: output a phrase "... ”
2. steps:
(1) Create a basic class,main;
(2) Create a memory space to store this paragraph
① first define attributes, Name,sex,age, etc.;
② re-defining the method requires recreating the new class class, but does not require a new MA method;
(3) Call system method/Library method println, realize output result
① creates a new object in the original class and calls the method in the new class class;
② run, output all the content.
//--------------------------------------------------
Using annotations to implement program logic
Think of the logic of the program in natural logic and write it down according to the specific steps
Write clearly every step
Second, Java data Type (ii)
1. The concept and role of arrays
(1) 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.
(2) Examples of arrays
int [] A = new int []{1,2,3,4,5};
string [] s = new string []{" Bear " , " little Bear ", " Little Bear" };
Employee [] e=new employee[10]; ( Employee is a custom class)
2. Declaration and creation of arrays
(1) Declaration of An array: There are two ways to declare an array:
Data Type [] array name for example:int [] A;
Data Type the name of the array [] For example: int a [];
Attention:
1. 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 types of a.
2. the array is only a null pointer after the declaration and cannot be used, and must be created if you want to use it.
(2) array creation : Three ways to create an array:
① allocates memory according to the specified length while declaring the array, but the element values in the array are the default initialization values
char[] Chary = new CHAR[10];
② declares an array and allocates memory while initializing it
int[] Ary1 = new Int[]{1, 2, 3, 4, 5};
③ is the same as the previous one, just a relatively brief syntax
Int[] Ary2 = {1, 2, 3, 4, 5};
(3) 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 ();
When creating an array, you must specify the length of the array, and no change is allowed once defined.
the length of the array, although 7, is actually given in memory to 8 locations, and the other one stores 7.
//---------------------------------------------------
in the in C, it is not possible to assign an array directly to another array;
in the in Java, this is syntactically permissible, but the actual effect is that two array references point to the same block of memory.
Int[] Ary1 = {2, 4, 6, 8, 10};
Int[] Ary2;
Ary2 = Ary1; allowed to assign this value
Third,Java operators
according to the function points, The operators in Java can be divided into four classes: arithmetic operators, bitwise operators, comparison operators, logical operators, arithmetic operators.
1. Arithmetic Operators
single-Mesh:+(take positive)-(minus) + +(self-increment 1) - - (self minus 1)
Binocular:+-*/%(remainder) (+ can also connect string)
Three mesh:a>b?true:false
2. Bitwise Operators :( converted into binary before Operation )
(1) with (&), non-(~), or (| ), XOR, or ( ^ );
(2) shift operators:<<, >>(signed right shift), >>> (unsigned Right shift);
(3) shift left 1 is equal to 2, right shift 1 is equal to divided by 2, more efficient than multiplication operation.
3. comparison operators:
= = ,<,>,<=,>=,!= ;
= = 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 true or false.
4. logical Operators
An expression for connecting two boolean types;
&& (double and),| | (double or), & (and), | (or), ^ (XOR), ! (non-operational);
Precautions:
1 . && & The results are the same, for false false
2.| | with the | The result of the operation is the same, for | the right side is involved in the operation, regardless of the value on the left. || , as long as the left side is true the right side is no longer calculated and returned directly true
3.&& | | can be understood as a physical short circuit. , multi-use in practice && and ||
Iv. Process Control
There are three types of processes in Java: sequential processes, branching processes, and cyclic processes.
1. Branch Statement If/else
(1) Conditional Statement if
if () {
statement block
}
(2) conditional statement If...else ...
if () {
statement block 1
}else{
statement block 2
}
(3) Conditional statement If...else If ...
if () {
statement block 1
}else if{
statement block 2
}else{
Statement block 3
}
2. Conditional Statement switch
(1) syntax format
Switch ( expression )
{
Case Value 1: statement block 1
...
Case value n: statement block N
Default: statement block n+1
}
(2) description
The value of the ① expression can only accept int,byte,char , short , enum ,JDK1.6 String can also, do not accept other types of values, do not allow duplicate case value;
② Switch once encountered the first case match, the program will jump to this label position;
③ begins the execution of all subsequent program code, regardless of whether the following case condition matches, until the break statement is encountered .
(3) Example
int x = 2;
Switch (x) {
Case 1:
System.out.println ("Monday");
Break
Case 2:
SYSTEM.OUT.PRINTLN ("Tuesday");
Break
Case 3:
SYSTEM.OUT.PRINTLN ("Wednesday");
Break
Default
System.out.println ("Sorry,i don ' t know");
}
3. Looping statements for, while,do/while
(1) Loop statement while
example: Calculate 1+2+3+ ... +10 and the
int x=1;
while (x<11) {
System.out.println ("x=" +x);
x + +;
}
(2) Circular statement Do...while
① syntax format
Do
{
Executes the statement block;
} while ( conditional expression );
② Example
int x=3;
do{
System.out.println ("x=" +x);
x + +;
} while (x<3);
(3) Loop statement for
Syntax format:
For ( initialization expression; cyclic conditional expression; post-loop action expression )
{
EXECUTE statement block
}
(4) interrupt statement for loop break ,continue
Function: interrupt statement; A label can be used in multiple loops to specify the specific loop of the interrupt.
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;thecontinue statement is used in the body of the loop statement to terminate a loop and skip the loop body continue A loop that is not executed under the statement to begin the next loop.
Java data Type (ii), Java operator, Process Control