1. An overview of the
1.1 Overview of Java
1.2 Relationship between JDK, JRE, and JVM
Note: 1.3 the Comment
- classification:
- One-line comment:
//
- Multi-line comments:
/ * * /
- Document notes:
/ * * * /
- One-line comment:
- Note: Multi-line comments cannot be nested
2. Basic grammar
2.1 Keywords and Identifiers
2.1.1 Use of keywords
-
Definition: A string (word) given a special meaning by the Java language for a specific purpose
-
Features: All letters in the keyword are lowercase
-
Specific keywords:
2.1.2 Use of identifiers
-
Definition: Any place you can name yourself is called an identifier.
-
Structures involved: package name, class name, interface name, variable name, method name, constant name
-
Rules :(must be followed. Otherwise, the compilation fails.)
-
Specification :(does not affect compilation, but requires compliance)
2.1.3 Use of variables
-
By data type:
-
Sort by declared location
-
Details:
- The integer:
byte
(1 byte =8bit) /short
(2 bytes) /int
(4 bytes) /long
(8 bytes) ① Byte range: -128 to 127 ② Declare long variables, which must end with “L” or “L” ③ Generally, int is used when defining integer variables. ④ Integer constants. The default type is int - floating-point:
float
(4 bytes) /double
(8 bytes) ① Floating point, representing the number with a decimal point ②Float means that the range of values is larger than long(3) When defining float variables, the variables must end with “f” or “f”. (4) When defining float variables, double is used. ⑤ Float constants, default type: double - character:
char
(1 character =2 bytes) ① Define variables of type CHAR' '
, can contain only one character. 2. Declare a character. Use Unicode values directly to represent character constants - The Boolean:
boolean
Can only take one of two values: true or false
- The integer:
-
Define variable format:
Datatype variable name = variable value; Or Data 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
An array of 3.
3.1 Basic knowledge of arrays
-
Understanding of Array: Array, is a number of the same type of data in a certain sequence of the collection, and the use of a name, and through the numbering of these data for unified management.
-
Array features:
-
Arrays are ordered;
-
Arrays are variables that refer to data types. The elements of an array can be either primitive or reference data types.
-
Creating an array object creates a contiguous space in memory;
-
Once the length of an array is determined, it cannot be modified.
-
-
Array classification:
- ① According to dimension: one-dimensional array, two-dimensional array,… ;
- ② By array element type: array of basic data type elements, array of reference data type elements.
3.2 One-dimensional Array
3.2.1 Declaration and initialization of one-dimensional arrays
/ / declare
int[] ids;
Static initialization: An array is initialized and an array element is assigned at the same time
ids = new int[] {1001.1002.1003.1004};
//1.2 Dynamic initialization: Array initialization and array element assignment are separated
String[] names = new String[5];
// Type inference
int[] arr4 = {1.2.3.4.5};
Copy the code
3.2.2 References to one-dimensional array elements
- Call through corner markers
// The index of the array starts at 0 and ends with the array length -1.
String[] names = new String[5];
names[0] = "Johnny";
names[1] = "Wang He";
names[2] = "Zhang Xueliang";
String wm = names[0];
Copy the code
3.2.3 Traversal of one-dimensional arrays
String[] names = new String[5];
/ / 1. The first one
for(int i = 0; i < names.length; i++){ System.out.println(names[i]); }/ / 2. The second
for(String name : names){
System.out.println(names[i]);
}
Copy the code
3.2.4 Default initialization values for one-dimensional array elements
// The array element is an integer: 0
// Array elements are floating point: 0.0
// Array elements are char: 0 or '\u0000' instead of '0'
// Array elements are Boolean: false
// Array elements are reference data types: null
Copy the code
3.3 Two-dimensional Array
3.3.1 How to understand two-dimensional arrays?
An array is a reference data type, and its elements can also be reference data types. If the elements of a one-dimensional array are of a one-dimensional array type, the array is called a two-dimensional array.
3.3.2 Declaration and initialization of two-dimensional arrays
// The correct way:
int[] arr = new int[] {1.2.3};// One dimensional array
// Static initialization
int[][] arr1 = new int[] [] {{1.2.3}, {4.5}, {6.7.8}};
// Dynamically initialize 1
String[][] arr2 = new String[3] [2];
// Dynamically initialize 2
String[][] arr3 = new String[3] [];// Also the correct way to write:
int[] arr4[] = new int[] [] {{1.2.3}, {4.5.9.10}, {6.7.8}};
int[] arr5[] = {{1.2.3}, {4.5}, {6.7.8}};// Type inference
// 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
4. Classes and objects
- Three main threads of object orientation:
- Java classes and their members: properties, methods, constructors; Code blocks, inner classes;
- The three main characteristics of object-oriented: encapsulation, inheritance, polymorphism, (abstractness);
- Other keywords: this, super, static, final, abstract, interface, package, import, etc.
4.1 Members of the class
4.1.1 Attributes, methods, constructors
// This is a class
public class Employee {
// Attributes, there are six private attributes.
// Public modifier: can be accessed by objects in this class as well as in other classes.
Attributes are usually private for encapsulation purposes
public String name;
//private is a modifier that means that it is accessible only within the class.
private String idCard;
private Integer age;
private Double height;
private Double weight;
private String address;
Constructor: modifier + class name.
/* 1. If the class constructor is not explicitly 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. * /
// No parameter constructor
// Generate constructor can be selected by Shift+Alt+ S shortcut key in idea
public Employee(a) {}// The constructor is heavily loaded
// Initialize through the constructor
public Employee(String name, String idCard, Integer age, Double height, Double weight, String address) {
this.name = name;
this.idCard = idCard;
this.age = age;
this.height = height;
this.weight = weight;
this.address = address;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdCard(a) {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public Integer getAge(a) {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getHeight(a) {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public Double getWeight(a) {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public String getAddress(a) {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString(a) {
return "Employee{" +
"name='" + name + '\' ' +
", idCard='" + idCard + '\' ' +
", age=" + age +
", height=" + height +
", weight=" + weight +
", address='" + address + '\' ' +
'} '; }}Copy the code
public class Test {
public static void main(String[] args) {
// Generate an object with a no-argument constructor
Employee employee = new Employee();
// Initialize through the constructor
Employee employee1 = new Employee("Zhang"."123".18.172.5.105d.Nanjing, Jiangsu Province);
// Public attributes can be accessed.String name = employee.name; }}Copy the code
4.1.2 Method of variable number parameters
- The f is involved in the argument distinction
- Parameter: a parameter declared in parentheses when a method is defined
- Argument: The data actually passed to the parameter when the method is called
Specific use:
- 1 Format of variable number parameter:
Data type... The variable name
- 2 When calling a method with a variable number of parameters, the number of arguments passed can be: 0, 1,2…
- 3 The method of variable number parameter is the same as the name of the Chinese method of this class, and the method of different parameters constitutes overload
- 4 An array with the same parameter type and the same method name does not constitute an overload. In other words, the two cannot coexist.
- 5 Variable parameter in method parameters, must be declared at the end
- 6 Variable parameter A maximum of one variable parameter can be declared in a method parameter.
public void show(String s){
System.out.println("show(String)");
}
//show(datatype... Variable name)
public void show(String ... strs){
System.out.println("show(String ... strs)");
for(int i = 0; i < strs.length; i++){ System.out.println(strs[i]); }}Copy the code
4.2 Three Characteristics
2 encapsulation
-
Why introduce 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.
-
The idea of encapsulation is embodied in concrete code:
- Make the class property XXX private and ** provide public methods to get **(getXxx) and set (setXxx) the value of this property;
- Do not expose private methods;
- Singleton pattern (privatizing the constructor);
- If you don’t want the class to be called outside the package, you can set the class to default.
-
Four permission modifiers
The modifier Inside the class The same package Subclasses of different packages Same project public Square root Square root Square root Square root protected Square root Square root Square root Chosun (default) Square root Square root private Square root
4.2.2 inheritance
-
Why should there be class inheritance? (Inheritance benefits)
- ① Reduce the code redundancy, improve the code reuse
- ② Facilitate the expansion of functions
- ③ It provides the premise for the use of polymorphism in the future
-
Description of inheritance in Java
- A class can be inherited by more than one subclass.
- Single inheritance of classes in Java: a class can have only one parent class
- Child and parent are relative concepts.
- The parent from which a subclass inherits directly is called the immediate parent. An indirectly inherited parent is called an indirect parent
- When a subclass inherits its parent, it gets the properties and methods declared in the direct parent and all indirect parent classes
-
An understanding of the java.lang.Object class
- If we do not explicitly declare a class’s parent, the class inherits from java.lang.Object;
- All Java classes (except java.lang.Object) inherit directly or indirectly from java.lang.Object;
- This means that the Java class has the functionality declared by the java.lang.Object class.
-
The difference between reloading and overloading
- What is method overwriting (
override
oroverwrite
)?- Overwrite: after a subclass inherits its parent class, it can override methods with the same name and parameters in the parent class.
- Overloading: Different implementations of the same method in the same class.
- Application: When a subclass object is created and a method with the same name and argument is called from the subclass object, the subclass overrides the method.
- What is method overwriting (
Holdings polymorphism
- 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)
- Use of polymorphism: With object polymorphism, at compile time, only the methods declared in the parent class can be called, but at run time, what we actually do is subclass override the methods of the parent class.
- Use the premise: ① class inheritance relationship ② method rewrite.
- Summary: Compile, look left; Run, look to the right.
4.3 the keyword
4.3.1 this
-
This can call properties and methods; This can be interpreted as: the current object or the object being created
-
Invoke properties and methods in a class
-
In a class method, we can call the current object property or method using either the “this. property “or “this. method.” But, more often than not, 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.
-
In the constructor of a class, we can use either the “this. property “or “this”. Method to call the property or method of the object 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.
-
-
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).
- ④ It is stipulated that”
This (parameter list)
“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
4.3.2 super
-
Super can call properties, methods, constructors; Can be interpreted as: superclass
-
Super calls properties, methods:
- We can do it atA method or constructor for a subclassIn the. By using”
Super. Properties
“Or”Method super.
“In the manner of,Explicitly calls a property or method declared in the parent class. However, in general, we tend to omit “super.” - Special case: when an attribute of the same name is defined in a subclass and a parent class, we must explicitly use “super “to call the attribute declared in the parent class. Property “, indicating that the property declared in the parent class is called.
- Special case: when a subclass overrides a method in its parent class, we must explicitly use “super “when we want to call the overridden method in the subclass. Method “, indicating that a method in the parent class is being overridden.
- We can do it atA method or constructor for a subclassIn the. By using”
-
Super call constructor:
- We can do it atIn the constructor of a subclassExplicit use”
Super (parameter list)
“In the manner of,Calls the specified constructor declared in the parent class - “
Super (parameter list)
The use of”Must be declared on the first line of the subclass constructor! - In the constructor of the class, we can only double one for “this “or “super”, not both
- On the first line of the constructor, without explicitly declaring “this(parameter list)” or “super(parameter list)”, the constructor for the hollow parameter of the parent class is called by default: super()
- “Super (parameter list)” is used in the constructor of at least one of the class’s constructors, calling the constructor in the parent class
- We can do it atIn the constructor of a subclassExplicit use”
4.3.3 the static
-
Static, used mainly to modify the internal structure of a class; Properties, methods, code blocks, inner classes.
-
Static modifies attributes:
- Static attributes vs. non-static attributes (instance variables)
- Static properties: Multiple objects of a class are created that share the same static variable. When a static variable is modified through one object, it causes other objects to call the static variable, which is modified.
- Dynamic properties: Multiple objects of a class are created, and each object has an independent set of non-static properties of the class. Changing a non-static property in one object does not result in changing the value of the same property in other objects.
- Note:
- Static variables are loaded as the class is loaded. Can pass”
Static variables
“ - Static variables are loaded before objects are created.
- Since classes are only loaded once, static variables also have only one copy in memory: the static domain of the method area.
- Static variables are loaded as the class is loaded. Can pass”
-
Static modifiers:
- Loading as the class is loaded can be done through”
Static methods
“ - In static methods, only static methods or properties can be called
- In non-static methods, you can call either static or non-static methods or properties
- Cannot be used in static methods
this
Keyword,super
The keyword
- Loading as the class is loaded can be done through”
4.3.4 final
- Finally, you can modify: classes, methods, variables.
- Final is used to modify a class that cannot be inherited by other classes, such as String, System, and StringBuffer.
- Final is used to modify a method to indicate that it cannot be overridden, as in getClass() of Object;
- Final used toModify variables:The variable is said to be a constant
-
Final modifier properties: Where assignments can be considered: ** explicitly initialized, initialized ina code block, initialized ina constructor; 支那Copy the code
-
Final modifies local variables: Especially when final modifies a parameter, it indicates that the parameter is a constant. When this method is called, an argument is given to the constant parameter. Once assigned, the parameter can only be used within the method body, but cannot be reassigned.Copy the code
-
'static final' is used to modify properties: global constantsCopy the code
-
4.3.5 abstract
-
Abstract, can be used to modify: class, method.
-
Abstract modifiers: Abstract classes
-
This class cannot be instantiated (new object);
-
The ** abstract class must have a constructor **, which is easy to call when the subclass instantiates;Copy the code
-
In the development, will provide the abstract class subclass, lets the subclass object instantiate, completes the related operation;Copy the code
-
-
Abstract: Abstract method
-
Abstract methods have only method declarations, but no method body.Copy the code
-
A class that contains an abstract method must be an abstract class. Conversely, an abstract class can have no abstract methods;Copy the code
-
** can be instantiated only if the ** subclass overrides all the abstract methods ** in its parent class.Copy the code
-
If a subclass does not override all the abstract methods in its parent class, it is also an abstract class and needs the abstract modifierCopy the code
-
4.3.6 interface
-
An interface is defined using an interface.
-
In Java, interfaces and classes are juxtaposed.
-
How to define an interface:
-
In addition to global constants and abstract methods, you can also define static methods and default methods.
Global constants: public static final. But in writing, you can omit it
Abstract method: public abstract
-
Interface cannot define a constructor! Means that the interface cannot be instantiated;
-
Interfaces are used by letting classes implement.
- If the implementation class overrides the abstract method in the interface, the implementation class can be instantiated
- If the implementation class does not override the abstract methods in the interface, the implementation class is still an abstract class
-
Java classes can implement multiple interfaces –> compensate for the limitations of Java’s single inheritance
- Format:
class AA extends BB implements CC,DD,EE
- Format:
-
Interfaces can be inherited from one another, and multiple interfaces can be inherited.
-
-
The specific use of interfaces reflects polymorphism
4.4 Use of packaging classes
-
Why was the wrapper class introduced? In order to give the variables of the primitive data type the characteristics of a class, a wrapper class was introduced.
-
Basic data type and corresponding wrapper class: wrapper class begins with uppercase
Basic data types A wrapper class byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Char -
Conversions between primitive data types, wrapper classes, and Strings