Comparison Operators
= Equal! = Not equal to <less than> greater than <= less than or equal
> = Greater than or equal
Note:
1. The result of the comparison operator is Boolean, that is, either true
Or false.
2. The comparison operator "=" cannot be mistakenly written as "="
Logical operators
& And (and) false & True False
| Or (OR) False | true
^ XOR (exclusive or) True ^ false true
! Not (not )! True False
& And (Short Circuit) false & True False
| Or (Short Circuit) False | true
& Features:
&: No matter whether the left side is true or false, all operations are performed on the right side.
&: If the left side is false, no operation is performed on the right side.
|: Both sides participate in Calculation
|: When the left side is true, no operation is performed on the right side.
<: The power of the number of digits multiplied by 2
>>: The power of the number of digits that are moved by dividing by 2.
6. Statements
Program Process Control
1. determine the structure
1. If statement
Ternary operation: Variable = (conditional expression )? Expression 1: expression 2;
(Benefit: It can simplify the if else code. Disadvantage: it is an operator, so
There must be a result after calculation)
2. Select Structure
1. Switch statement
Format:
Switch (expression ){
Case value 1:
Default:
}
Switch features:
A. There are only four types of statements: byte, short,
Int, char;
B. There is no order between case and default. First execute the first
Case. If no matching case exists, run default;
C. There are two scenarios for ending the switch statement: encounter break, execute
The row ends with the switch statement;
D. If the matched case or default does not have a corresponding
Break, then the program continues to run down and runs executable statements
End with break or switch.
3. Loop Structure
1. While (condition expression) {execution statement}
2. Do {execution statement;} while (conditional expression );
Do While is characterized by the fact that the loop body is executed at least once regardless of whether the condition is met or not.
.
3.
For (INT I = 0; I <5; I ++ ){
For (Int J = I; j <= 5; j ++ ){
System. Out. Print ("");
}
For (int K = 0; k <I; k ++ ){
System. Out. Print ("*");
}
System. Out. println ();
}
7. Functions
Function Definition: a function is an independent small part of a class with specific functions.
Program.
A function is also called a method.
Function Format:
Modifier Return Value Type Function Name (parameter type parameter ){
Execute the statement;
Return 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: it is a variable used to store the actual information passed to the function when a function is called.
Parameters.
Actual parameter: the specific value passed to the form parameter.
Return: Used to end a function.
Return Value: this value is returned to the caller.
Features:
1. define functions to encapsulate functional code.
2. This function is easy to reuse.
3. A function can only be called for execution.
4. If the function does not return a specific value, use the keyword void table for the return value type.
If the return statement in the function can be omitted in the last line
Note: 1. Only functions can be called in a function and cannot be defined in a function.
2. When defining a function, the function result should be returned to the caller and transferred to the caller
.
Function overload)
Concept of heavy load
In the same class, more than one function with the same name is allowed, as long as their parameters
The number or parameter type is different.
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 ;}
When to use overload?
When the defined functions are the same, but the unknown content involved in the operation is the same. Then
Define a function name to indicate the function.
To distinguish multiple functions with the same name.
8. Array
Array Definition
Concept: a collection of the same type of data. In fact, arrays are a container.
Array advantages: You can automatically number the elements in the array from 0 to facilitate this operation.
Some elements.
Format 1:
Element type [] array name = new element type [number of elements or array length];
Format 2:
Element type [] array name = new element type [] {element, element ,......}
When a Java program is running, it needs to allocate space in the memory.
Rate, the space is divided by different regions, because each region has a specific
Data processing and memory management.
Stack memory:
Used to store local variables. when data is used up, the occupied space is automatically released.
Heap memory:
1. arrays and objects are stored in heap memory in instances created through new.
2. Each object has a memory address value.
3. All variables in the object have default initialization values.
4. If the object is not used, it will be recycled by the Garbage Collector within an uncertain period.
Boolean default value: false
Method Area:
Local Method Area:
Register:
Array Operations: to obtain elements in an array is the most common. Traversal is usually used.
Int [] arr = new int [3];
For (INT x = 0; I <3; X ++) {syso (ARR [x]);} // disappears when the for loop is used up.
.
An attribute in the array can be used to obtain the number of array elements. Length
Usage: the array name. length can get the length of the array.
Obtains the maximum and minimum values in the array.
1. Get the maximum value
Idea: 1. Get the most value and compare it. Each comparison will have a large
Because the value is uncertain, a variable is used for temporary storage. 2. Let the Array
Each element in is compared with the value in this variable. If the value is greater than the variable
The variable is used to record a large value. 3. When all elements are compared
The maximum value in the array is stored in the variable.
Step: 1. Define the variable and initialize it to any element in the array.
2. traverse the Array Through the loop statement.
3. Define the judgment condition during the variable process. If the element ratio variable is retrieved
And assign the value to the variable.
You need to define a function. To improve reusability.
1. Clear the result. The maximum element int in the array.
2. Unknown content: An array. Int [].
Public class test {
Public static void main (string [] ARGs ){
Int [] arr = {1, 2, 3, 4, 5, 8, 6 };
Int max = getmax (ARR );
Syso (max );
}
Public static int getmax (INT [] ARR ){
Int max = arr [0];
For (INT x = 1; x <arr. length; X ++ ){
If (ARR [x]> MAX)
Max = arr [x];
}
Return Max;
}
}
Another way to obtain the maximum value.
Can I initialize a temporary variable to 0? Yes. In this way
Starts as any subscript in the array.
Public class test {
Public static void main (string [] ARGs ){
Int [] arr = {1, 2, 3, 4, 5, 8, 6 };
Int max = getmax (ARR );
Syso (max );
}
Public static int getmax (INT [] ARR ){
Int max = 0;
For (INT x = 1; x <arr. length; X ++ ){
If (ARR [x]> arr [Max])
Max = X;
}
Return arr [Max];
}
}
2. Obtain the minimum value.
Public class test {
Public static void main (string [] ARGs ){
Int [] arr = {1, 2, 3, 4, 5, 8, 6 };
Int min = getmax (ARR );
Syso (min );
}
Public static int getmin (INT [] ARR ){
Int min = 0;
For (INT x = 1; x <arr. length; X ++ ){
If (ARR [x] <arr [Max])
Max = X;
}
Return Min [Max];
}
}
Sort the given array:
The inner loop ends once, and the maximum value appears at the first subscript position.
Class arraytest {
Public static void main (string [] ARGs ){
Int [] arr {5, 45, 8, 4, 3, 5, 7 };
// Print before sorting;
Printarray (ARR );
// Sort
Selectsort (ARR );
// Print after sorting;
Printarray (ARR );
}
Public static void selsectsort (INT [] ARR ){
// You do not need to traverse the last subscript.
For (INT x = 0; x <arr. Length-1; X ++ ){
For (INT y = x + 1; y <arr. length; y ++ ){
// Change the greater than sign to a smaller than sign, and sort it in reverse order.
If (ARR [x]> arr [y]) {
Int temp = arr [x];
Arr [x] = arr [y];
Arr [y] = temp;
// Second method, encapsulate this and call
Swap (ARR, x, y );
}
}
}
}
Public static void printarray (INT [] ARR ){
System. Out. Print ("[");
For (INT x = 0; x <arr. length; X ++ ){
If (X! = Arr. Length-1)
Syso (ARR [x] + ",");
Else
Syso (ARR [x] + "]");
}
}
}
Bubble Sorting:
Public static void bubblesort (INT [] ARR ){
For (INT x = 0; x <arr. Length-1; X ++ ){
For (INT y = 0; y <arr. length-x-1; y ++) //-X: Let each ratio
Relatively less elements,-1: avoid cross-border below.
{
If (ARR [y] <arr [Y + 1]) {
Int temp = arr [y];
Arr [y] = arr [Y + 1];
Arr [Y + 1] = temp;
// Method 2 encapsulate the call
Swap (ARR, Y, Y + 1 );
}
}
}
}
We can conclude from the above: No matter what sorting, all elements that meet the conditions must be sorted.
Position replacement. So we can extract the same code and encapsulate it separately
A function.
Public static void swap (INT [] arr, int A, int B ){
Int temp = arr [a];
Arr [a] = arr [B];
Arr [B] = temp;
}
Hexadecimal conversion:
Class arraytest5 {
Public static void main (string [] ARGs ){
Tobin (6 );
System. Out. println (integer. tobinarystring (60); // locate 60
Binary
}
// Convert decimal to binary
Public static void Tobin (INT num ){
Stringbuffer SBF = new stringbuffer ();
While (Num> 0 ){
SBF. append (Num % 2 );
Num = num/2;
}
System. Out. println (SBF. Reverse ());
}
// Convert decimal to hexadecimal
Public static void tohex (INT num ){
/*
Int n1 = num & 15;
N1-10 + 'a ';
Int temp = num >>> 4;
Temp = num & 15;
*/
Stringbuffer SBF = new stringbuffer ();
For (INT x = 0; x <8; X ++ ){
Int temp = num & 15;
If (temp> 9 ){
// Syso (char) (temp-10 + 'A '));
SBF. append (char) (temp-10 + 'A '));
Else
SBF. append (temp );
Num = num >>> 4;
}
Syso (SBF. Reverse ());
}
}
}
Hexadecimal conversion using the lookup table
/*
0 1 2 3 4 5 6 7 8 9 a B C D E F = elements in hexadecimal notation
.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Table query: stores all elements temporarily and establishes a ing relationship.
The value after each & 15 is used as an index to check the created table. You can find the corresponding element.
. This is much simpler than-10 + 'A.
It can be defined in the form of arrays.
The following result is reversed. If you want to forward the result, you can use the stringbuffer
Reverse function implementation. You can also store data using arrays.
*/
Public class test {
Public static void main (string [] ARGs ){
Tohex (60 );
}
Public static void tohex (INT num ){
Char [] CHS =
{'0', '1', '2', '3', '4', '5', '6', '7', '8 ', '9', 'A', 'B', 'C', 'D ',
'E', 'F '}
Char [] arr = new char [8];
Int Pos = arr. length;
While (num! = 0 ){
Int temp = num & 15;
// Syso (CHS [temp]);
Arr [-- POS] = CHS [temp];
Num = num >>> 4;
}}
// Arr array traversal for data storage
For (INT x = Pos; x <arr. length; X ++ ){
Syso (ARR [x] + ",");
}
}
}