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:/ * * * /
  • 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

  1. By data type:

  2. Sort by declared location

  3. 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:booleanCan only take one of two values: true or false
  4. Define variable format:

    Datatype variable name = variable value; Or Data type variable name; Variable name = variable value;

  5. 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

  1. 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.

  2. Array features:

    1. Arrays are ordered;

    2. Arrays are variables that refer to data types. The elements of an array can be either primitive or reference data types.

    3. Creating an array object creates a contiguous space in memory;

    4. Once the length of an array is determined, it cannot be modified.

  3. 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 = {};
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){
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}, {}, {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:
    1. Java classes and their members: properties, methods, constructors; Code blocks, inner classes;
    2. The three main characteristics of object-oriented: encapsulation, inheritance, polymorphism, (abstractness);
    3. 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;
    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"., 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){
//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

  1. 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.
  2. 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.
  3. 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

  1. 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
  2. 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
  3. 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.
  4. The difference between reloading and overloading

    1. What is method overwriting (overrideoroverwrite)?
      • 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.
    2. 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.

Holdings polymorphism

  1. 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)
  2. 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.
  3. Use the premise: ① class inheritance relationship ② method rewrite.
  4. Summary: Compile, look left; Run, look to the right.

4.3 the keyword

4.3.1 this

  1. This can call properties and methods; This can be interpreted as: the current object or the object being created

  2. 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.

  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).
    • ④ 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

  1. Super can call properties, methods, constructors; Can be interpreted as: superclass

  2. Super calls properties, methods:

    1. 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.”
    2. 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.
    3. 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.
  3. Super call constructor:

    1. 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
    2. Super (parameter list)The use of”Must be declared on the first line of the subclass constructor!
    3. In the constructor of the class, we can only double one for “this “or “super”, not both
    4. 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()
    5. “Super (parameter list)” is used in the constructor of at least one of the class’s constructors, calling the constructor in the parent class

4.3.3 the static

  1. Static, used mainly to modify the internal structure of a class; Properties, methods, code blocks, inner classes.

  2. Static modifies attributes:

    • Static attributes vs. non-static attributes (instance variables)
    1. 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.
    2. 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.
  3. Static modifiers:

    1. Loading as the class is loaded can be done through”Static methods
    2. In static methods, only static methods or properties can be called
    3. In non-static methods, you can call either static or non-static methods or properties
    4. Cannot be used in static methodsthisKeyword,superThe keyword

4.3.4 final

  1. Finally, you can modify: classes, methods, variables.
  2. Final is used to modify a class that cannot be inherited by other classes, such as String, System, and StringBuffer.
  3. Final is used to modify a method to indicate that it cannot be overridden, as in getClass() of Object;
  4. 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

  1. Abstract, can be used to modify: class, method.

  2. 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
  3. 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

  1. An interface is defined using an interface.

  2. In Java, interfaces and classes are juxtaposed.

  3. How to define an interface:

    1. 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

    2. Interface cannot define a constructor! Means that the interface cannot be instantiated;

    3. 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
    4. Java classes can implement multiple interfaces –> compensate for the limitations of Java’s single inheritance

      • Format:class AA extends BB implements CC,DD,EE
    5. Interfaces can be inherited from one another, and multiple interfaces can be inherited.

  4. The specific use of interfaces reflects polymorphism

4.4 Use of packaging classes

  1. 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.

  2. 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
  3. Conversions between primitive data types, wrapper classes, and Strings