#Java basic programming and mind mappingDirectory:

Java learning map # 1, Java basic syntax 1. Keywords and identifiers 2. Variable classification 3. Operators 4. Flow control # 2. Array 1. Object-oriented Java classes and their members: Encapsulation, inheritance, polymorphism# 1 Java basic syntax 1. Keywords and identifiers

Keyword and identifier

2. Classification of variables

Defining variable formats

Variable type Variable name = value;

Variable type Variable name; Variable name = variable value;

Note the use of variables

  • ① Variables must be declared before they are used
  • ② Variables are defined in their scope. It is valid in scope. In other words, out of scope, out of scope
  • ③ You cannot declare two variables with the same name in the same scope

Basic data variable operation rules

Automatic type conversion: Conclusion: When a variable of a small data type is evaluated with a variable of a large data type, the result is automatically promoted to a large data type. Byte, char, short –> int –> long –> float –> double

Note: at this time the capacity size refers to the number of large and small range. For example, float has more capacity than long

Cast:

  • ① need to use strong character :()
  • Note: Casting may lead to loss of accuracy.

3. The operator Arithmetic operators: + – + – * / % (front)++ (back)++ (front)– (back)– + Special note:

1.(front)++ : Increments 1 before calculating

(back)++ : Calculates first and then increases by 1

2.(before)– : First subtract 1, then calculate

(after)– : calculate first, then subtract 1

3. The hyphen: + : can only be used between strings and other data type variables.

Copy operator: = += -= *= /= %=

1. The result of the operation does not change the data type of the variable itself

2. In development, if you want a variable to implement the +1 operation, how many ways? Int num = 10;

Num = num + 1; Num += 1; // num++; (recommended)

Comparison operator: ==! = > < >= <= instanceof

1. The result of the comparison operator is of Boolean type

2.> < >= <= : Can only be used between data of numeric type.

  1. = = and! =: can be used not only between numeric type data, but also between variables of other reference types.

Logical operators: && and | | |! ^ Of particular note:

1. Logical operators operate on Boolean variables. The result is also a Boolean type

2. Distinguish & from &&

Similarity 1: Ampersand and ampers& have the same result. Similarity 2: When the left side of the symbol is true, both perform the operation on the right side of the symbol. Difference: When the left side of the symbol is false, the ampersand continues to perform the operation on the right side of the symbol. Am& no longer performs the operation to the right of the symbol. During development, it is recommended to use &&

3. Distinguish between: | and | |

Similarities 1: | and | | the operation results of the same similarities 2: when the symbol on the left is false, symbol to the right of the operation performed both are different between 3: when the symbol on the left is true | continue symbol to the right of the operation, and | | symbol to the right of the operation performed no longer development, it is recommended to use | |

Operator: < < > > > > > & | ^ ~

Special note:

Bitwise operators operate on integer data << : * 2 for every bit to the left within a certain range

>>: Within a certain range, every 1 bit to the right corresponds to the /2 ternary operator: (conditional expression)? Expression 1: Expression 2 is specified

instructions

  • ① The result of a conditional expression is a Boolean
  • ② Depending on whether the conditional expression is true or false, decide whether to execute expression 1 or expression 2.

If the expression is true, expression 1 is executed. If the expression is false, expression 2 is executed.

  • ③ The requirements of expression 1 and expression 2 are identical.
  • ④ Ternary operators can be nested

So wherever you can use a ternary operator, you can rewrite it as if-else and otherwise it doesn’t work.

If the program can use both ternary operators and if-else structures, the ternary operators are preferred. Reason: Simple and efficient.

4. Process control Branch structure: 1. If-else condition judgment structure

A:if(conditional expression) {execute expression}Copy the code
Structure 2: Choose one of the twoif(conditional expression) {execute expression1
}else{execute expression2
}
Copy the code
Structure 3: n choose oneif(conditional expression) {execute expression1
}else if(conditional expression) {execute expression2
}else if(conditional expression) {execute expression3}...else{execute expression n}Copy the code

2. Switch-case Select structure

switch(expression) {caseconstant1: Execute statement1;
break;
caseconstant2: Execute statement2;
break; ...default: Execute statement n;break;
}
Copy the code

Cycle structure:

1. Four elements of the circular structure

1 Initialization conditions

② loop condition – > Boolean ③ loop body ④ Iteration condition Description: In most cases, loop ends because ② loop condition returns false.

2. Three cycle structures:

2.1 For loop structure

for(1); (2); ④){③} Execution process: ① - ② - ③ - ④ - ② - ③ - ④ -... - 2.Copy the code

2.2 While loop structure

1.while(2) {(3); (4); } Execution process: ① - ② - ③ - ④ - ② - ③ - ④ -... - ② Description: WritewhileThe loop must be careful not to lose iteration conditions. Once lost, it could lead to an endless loop!Copy the code

Summary of for and while loops:

In development, we basically choose between for and while to implement the loop structure. For loops and while loops are interchangeable!

Difference: The scope of the initialization condition part of the for loop is different from that of the while loop.

2.3 Do while loop structure

1.do{(3); (4); }while(2)); Execution process: ① - ③ - ④ - ② - ③ - ④ -... - 2.Copy the code

Description:1. The do-while loop executes the body at least once! 2. In development, use for and while more. Less use of do-while # 2 1. Array Overview ** Array (Array) is a collection of data of the same type arranged in a certain order, and named by a name, and unified management of these data through numbering.

2. Array related concepts:

An array of

Element subscript, index array length: the number of elements

3. Array features:

Arrays are ordered

Arrays are variables that refer to data types. The elements of an array can be either primitive data types or reference data types and when you create an array object you create a whole contiguous space in memory and you can’t change the length of the array once you’ve determined it. **4. Array classification:

  • ① Two-dimensional number: one-dimensional array, two-dimensional array,…
  • ② According to the type of array elements: array of basic data type elements, array of reference data type elements

2. One-dimensional arrays 1. Declaration and initialization of one-dimensional arrays The wrong way:

// int[] arr1 = new int[];
// int[5] arr2 = new int[5];
// int[] arr3 = new int[3]{1,2,3};
Copy the code

**2. References to one-dimensional array elements: ** is called with corner markers. Array markers (or indexes that start at 0 and end with array length -1)

3. Array property: length

System.out.println(names.length);/ / 5
System.out.println(ids.length);
Copy the code

Description: Once an array is initialized, its length is fixed. Arr. length Once the array length is specified, it cannot be modified.

4. Traversal of one-dimensional arrays

for(int i = 0; i < names.length; i++){ System.out.println(names[i]); }Copy the code

5. Default initialization values for one-dimensional array elements

Array elements are integer: 0 Array elements are floating point: 0.0 Array elements are char: 0 or ‘\u0000’, not ‘0’ Array elements are Boolean: false Array elements are reference data type: NULL

3. Two-dimensional arrays 1. How to understand two-dimensional arrays?An array that is A reference data type can also be A reference data type an element of A one-dimensional array A if it’s still A one-dimensional array, then array A is called A two-dimensional array.

2. Declaration and initialization of two-dimensional arrays The wrong way:

// String[][] arr4 = new String[][4];
// String[4][3] arr5 = new String[][];
/ / int [] [] arr6 = new int [4] [3] {{1, 2, 3}, {4, 5}, {June}};
Copy the code

3. How to call a two-dimensional array element:

System.out.println(arr1[0] [1]);/ / 2
System.out.println(arr2[1] [1]);//null
Copy the code
	arr3[1] = new String[4];
	System.out.println(arr3[1] [0]);
System.out.println(arr3[0]);//
Copy the code

4. Iterate over two-dimensional array elements

for(int i = 0; i < arr.length; i++)for(int j = 0; j < arr[i].length; j++){ System.out.print(arr[i][j] +"")}Copy the code

1. Array creation and element assignment: Yang Hui triangle (two-dimensional array), loop number (two-dimensional array), 6 numbers, randomly generated between 1-30 and not repeated.

2. Arrays for numeric types: maximum, minimum, sum, average, etc

3. Array copy and copy

int[] array1,array2;
array1 = new int[] {1.2.3.4};
Copy the code

3.1 assignment:

array2 = array1; Assigns the address value of the array held by Array1 to Array2 so that array1 and Array2 both point to the same array entity in the heap space.

3.2 copy:

array2 = new int[array1.length];
for(int i = 0; i < array2.length; i++){ array2[i] = array1[i]; }Copy the code

We give Array2 a new array space in the heap using new. Assigns the values of the elements in Array1 to array2 one by one.

1. Understand:

  • ① Defined in the java.util package.
  • ② Arrays: Provides many ways to manipulate Arrays.

2. Use:

//1. Boolean equals(int[] a,int[] b): check whether two arrays are equal.
		int[] arr1 = new int[] {1.2.3.4};
		int[] arr2 = new int[] {1.3.2.4};
		boolean isEquals = Arrays.equals(arr1, arr2);
		System.out.println(isEquals);
		
		//2.String toString(int[] a): Outputs array information.
		System.out.println(Arrays.toString(arr1));
		
			
		//3.void fill(int[] a,int val): fill the array with the specified value.
		Arrays.fill(arr1,10);
		System.out.println(Arrays.toString(arr1));
		

		//4. Void sort(int[] a): sort the array.
		Arrays.sort(arr2);
		System.out.println(Arrays.toString(arr2));
		
		//5.int binarySearch(int[] a,int key)
		int[] arr3 = new int[] {- 98..- 34.2.34.54.66.79.105.210.333};
		int index = Arrays.binarySearch(arr3, 210);
		if(index >= 0){
			System.out.println(index);
		}else{
			System.out.println("Not found");
		}
Copy the code

# 3. Object-oriented# Java classes and their class members1. One of two important structures in class design: attributes

Contrast: attributes vs local variables

1. Similarities:

1.1 Define variable format: Data type Variable name = variable value

1.2 declare first, then use 1.3 variables are their corresponding scope

2. Differences:

2.1 Different attributes for declared positions in a class: local variables defined directly in a {} pair of classes: variables declared in methods, method parameters, code blocks, constructor parameters, and constructors

2.2 Different attributes of permission modifiers: You can specify the permission and use permission modifiers when declaring attributes. Common permission modifiers: private, public, default, protected — > encapsulation Nowadays, when you declare a property, you just use absence. Local variables: Permission modifiers cannot be used.

2.3 Default Initialization: Attribute: A class attribute, based on its type, is initialized by default.

Int (byte, short, int, long: 0)

Char: 0 (or ‘\u0000’)) Boolean (Boolean: false)

Reference data type (class, array, interface: NULL) Local variable: no default initialization value. This means that we must explicitly assign a local variable before calling it. Specifically: when the parameter is called, we assign it.Copy the code

2.4 Loading location in memory: Properties: loaded into heap space (non-static) Local variables: loaded into stack space

2. In class design, the second of two important structures is: methods: describe what a class should do.

1. Example:

  • public void eat(){}
  • public void sleep(int hour){}
  • public String getName(){}
  • public String getNation(String nation){}

2. Method declaration: Permission modifier return value type Method name (parameter list){method body}

3. Description: 3.1 About Permission modifiers: The default permission modifiers use public first

Java provides four permission modifiers: private, public, default, protected –> Encapsulation

3.2 Return value types: Return value vs no return value

3.2.1 If a method returns a value, the type of the return value must be specified when the method is declared. Also, in a method, the return keyword is used to return a variable or constant that refers to the type: “return data”. If the method does not return a value, void is used when the method is declared. In general, there is no need to use return in methods that do not return a value. However, if you do, you can only use “return;” To end this method.

2. The function of a constructor:

Initializing object information Usage instructions: 1. If no explicit constructor is defined, the system provides an empty parameter constructor 2 by default. Define the format of constructors: Permission modifier Class name (parameter list){} 3. Multiple constructors defined in a class, each constituting an overload 4. Once we have explicitly defined the constructor for the class, the system no longer provides the default empty parameter constructor 5. There is at least one constructor in a class.

# Three features (encapsulation, Inheritance, polymorphism) Object oriented feature 1: encapsulation and hiding

1. Why encapsulation? Our program design pursues “high cohesion, low coupling”. High cohesion: The internal data manipulation details of the class are done by itself, without external interference; Low coupling: Only a few methods are exposed for use.

Hide the internal complexity of objects and expose only simple interfaces. Easy to call outside, so as to improve the system scalability, maintainability. In layman’s terms, hide what needs to be hidden and expose what needs to be exposed. That’s the idea of encapsulation.

2. Encapsulation thought concrete code embodiment: Embodiment 1: the class attribute XXX private (), at the same time, provide a public (public) method to get (getXxx) and set (setXxx) the value of this attribute

private double radius;
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(a){
return radius;
}
Copy the code

Embodiment 2: Private method embodiment 3: singleton pattern (private constructor) Embodiment 4: If you do not want the class to be called outside the package, you can set the class to default.

3. The four permission modifiers specified by Java 3.1 Permission in ascending order: private < default < protected < public 3.2 Specific scope of modification: 3.3 Permission modifier Structure description:

4. All permissions can be used to modify the internal structure of a class: properties, methods, constructors, inner class to modify a class, can only use: default, public

Object oriented feature two: inheritance

Class A extends B{}

  • A: Subclass, derived class, subclass
  • B: Superclass, superclass, base class, superclass

2. What are the differences when subclasses inherit from their parents?

2.1 Embodiment: Once subclass A inherits parent class B, subclass A acquires all the attributes and methods declared in parent class B.

In particular, if a subclass inherits a property or method declared private in the parent class, it still thinks it has the private structure in the parent class. It is simply because of encapsulation that a human cannot call the parent structure directly.

2.2 After a subclass inherits its parent class, it can also declare its own unique attributes or methods: to realize the extension of functions.

The relationship between subclasses and superclasses is different from the relationship between subsets and sets. Extends extends in Java 3

3.1. A class can be inherited by more than one subclass.

3.2. Single inheritance of Classes in Java: a class can have only one parent class 3.3. Child and parent are relative concepts. 3.4. The parent class directly inherited by a subclass is called the immediate parent class. An indirectly inherited parent class is called an indirect parent class 3.5. When a subclass inherits its parent class, it obtains the attributes and methods declared in the direct parent class and the indirect parent class

Object oriented property three: polymorphism

1. Understanding of polymorphism: it can be understood as multiple forms of one thing.

2. What is polymorphism: Object polymorphism: a reference from a parent class to an object of a child class (or a reference from an object of a child class to a parent class) example:

Person p = new Man();
Object obj = new Date();
Copy the code

3. Use of polymorphism: virtual method invocation With object polymorphism, at compile time, we can only call methods declared in the parent class, but at run time, we actually perform subclasses overriding the parent class’s methods. Summary: Compile, look left; Run, look to the right.

4. Premises for the use of polymorphism:

  • Class inheritance
  • ② Rewrite the method

6. Notes on the use of polymorphism: Object polymorphism applies only to methods, not properties (see left for compilation and execution)

# keyword keyword: this 1. Can call the structure: attribute, method; The constructor

2. This calls properties and methods: this means: the current object or the object being created

2.1 In class methods, we can use the “this. attribute “or “this”. Method “to call the current object property or method. But usually, we omit “this.” In special cases, we must explicitly use “this “if the method parameter has the same name as the class attribute. “, indicating that the variable is an attribute, not a parameter.

2.2 In class constructors, we can use the “this. property “or “this”. Method “to call an object property or method that is currently being created. But, more often than not, we omit “this.” In particular, we must explicitly use “this “if the constructor parameter has the same name as the class attribute. “, indicating that the variable is an attribute, not a parameter.

3. This calls the constructor:

  • (1) We can explicitly call other constructors in the class using the “this “method
  • ② The constructor cannot call itself with “this”
  • ③ If a class has n constructors, at most n-1 constructors use this(parameter list).
  • ④ Specifies that “this “must be declared on the first line of the current constructor
  • ⑤ Inside the constructor, at most one “this “can be declared, which can be used to call other constructors

Abstract: class, method 2. Abstract Modifiers: abstract classes

Class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation: class instantiation

An abstract method is only a method declaration, no method body contains an abstract method class, must be an abstract class. Conversely, an abstract class can have no abstract methods. If a subclass overrides any abstract method in its parent class, it can instantiate it. If it does not override any abstract method in its parent class, it is an abstract class and needs the abstract modifier 3. Note:

1. Abstract cannot be used to modify: attributes, constructors, etc

2. Abstract cannot be used to modify private methods, static methods, final methods, or final classes