Java foundation notes summary

Foreword

1.JDK, differences and connections between JRE, JVM

  1. JDK is the Java Development Kit, JRE is the Java runtime environment, JVM is Java Virtual Machine
  2. These three relationships are nested, JDK includes the JRE, JRE included JVM

A, static keyword

1. Static is the first to be in the class (JVM mechanism) loading
2. Action:
(1) create independent of the specific object or variable domain method
(2) can not create an object, you can also directly call the method used and the order attributes defined
form a static code block (3) is used to optimize the performance of the program
3. Loading order
(1) when the initial load class, will be performed in static blocks, and only once
4. Static unique role, the modified method is independent of static variables or any object class, object class instance of shared, only one copy of memory
5. Note
(1) is loaded and initialized when the class is first used, according to assignment requires rear branch
(2) Static local variables are not allowed to modify
6. And the difference between ordinary variables
Static (1): initialization sequence static member variables are initialized in the order defined in
(2) non-static objects are owned by the sub-created object is initialized when there are multiple copies of each object has each copy does not affect
7. Internal static type
(1) can not be used any non-static member variables and methods of the enclosing class
(2), when the currently loaded class, the class is not static loaded into internal memory. Only static inner classes can call the static inner classes will start loading and initialization, please mechanism will ensure that the JVM can only instantiated once, JVM provides a thread-safe support
8. Static static guide package -import
(1) can be imported to specify a static resource class
(2) may not use the name of the class call the class Static variables directly (but developers do not recommend it)
9. Static and final


Second, the abstract classes and interfaces

1, an abstract class (abstract)

  1. Abstract classes and classes have the same construction method, the static modifier static
    1, may be non-abstract methods abstract class
    2, abstract class constructor can not be directly used to create an object instance
    3, an abstract class can not final, private modification, public access type , protected
    . 4, an external static abstract class can not be modified, but the internal abstract class can be declared static, as follows
    abstract class A{ static abstract class B{ public abstract void decise(); } }
  2. Abstract method
    1, the method is not an abstract method body
    2, abstract methods must be modified with the abstract keyword
    3, abstract method must be public and protected, DEFAULT public
    . 4, an abstract class is an abstract class must method, an ordinary method in the abstract may class
  3. Significance of
    1 abstract class definition is to inherit
    2, the constructor of an abstract class can not be directly instantiated to create objects involved upward transition, the role is to quilt class called
    3, ordinary class inherits the abstract class must implement the abstract abstract superclass A method, if not achieved, a subclass is defined as the need to abstract class

2, the interface (interface)

  1. The basic concept of
    a process interface default public abstract type

    1. 1 However, there are exceptions, as follows
    2. interface A{default void b(){System.out.println("default方法");}}

    2, member variables and the default type of interface is only public static final (skippable) variable type
    3, can not have constructor interface
    4, interface methods are implicitly designated public abstract method, and is only
    5, Interface can achieve multiple inheritance, separated by commas

3, the difference between abstract classes and interfaces

  1. Design
    an abstract class to add new hair, you can add specific implementation (similar to the conventional method) directly in an abstract class, subclass without changing the
    2, the interface has been changed, all the realization of this interface class requires a corresponding change

  2. Design point
    1, abstract abstract class for the entire class, including attributes, behaviors
    2, the interface is an abstract behavior, abstract local behavior


Third, inheritance

1. Inheritance

  1. Inheritance is a subclass inherits the properties and behavior of the parent class, subclass can directly access the parent's private properties and behavior Central Africa
  2. Improve the reusability of code, so that the relationship between classes and generating class
  3. Only supports single inheritance does not support multiple inheritance

2. The member variables

  1. If the variable is not a member of the same name, called the father of the child object class can be called directly
  2. If the member variable of the same name, then access the parent class member variables need to use super. Parent class member variable name, this. Subclass member variable name

3. How to access the parent class private member variables

  1. Non-proprietary variables can be accessed directly
  2. The use packaging methods get private access to the variables and set

4. Members method

  1. If the method is not a member of the same name in the subclass call the parent class object can be called directly, because the object when calling the method, will first have to find no corresponding method in a subclass, if there is a subclass of the first class on the implementation of the method, when the subclass does not exist on the respective method is the parent class
  2. If the members of the same name, then the method
    1, the same method name, return type, parameter list is not the same (first look at the sub-class, subclass, no parent to go look for)
    2, the method name and return type, parameter lists are the same, rewriting (override) method

5. Rewrite the meaning of

  1. Inherits the parent class functions and can be expanded as needed subclass
  2. When rewriting plus super super method (); Indicates call the parent class member method

6. Rewrite Note

  1. Must rewrite the method name and parameter list consistent
  2. Method subclass overrides the parent class method, permission subclass greater than or equal to the parent permissions
  3. When overriding subclass of less than or equal to the return type of the return value of the parent type, type of exception subclass thrown exception type is equal to less than the parent class thrown

7. The construction method
1. If the main method instantiates a subclass object, then go perform the default constructor subclass initialization, the first time a code is added super default constructor (), do not write this also exist, and must be of a code
2. the method of tectonic member variable is initialized, the initialization process subclass, must perform the initializing operation of the parent class, the subclass constructor of a default adding super (); indicates that the call the parent class constructor after the parent class member variables are initialized before they can be used to subclass

class Father {
        // 父类的无参构造方法。
        public Father(){
            System.out.println("父类构造方法执行");
        }
    }
    class Son extends Father {
        // 子类的无参构造方法。
       public Son(){
           System.out.println("子类构造方法执行了");
       }
    }
    public class ConstructionDemo {
        public static void main(String[] args) {
            // 创建子类对象
            Son s = new Son();

        }
    }
//结果是
//父类构造方法执行
//子类构造方法执行了

8.super and this usage

  1. 1, super: storage space identifier on behalf of the parent class (can be understood as a reference to his father).
  2. 2, this: representing the current object reference (who calls who represents)
  3. 3, super, and this can not appear in a constructor function, because this is bound to call other constructors, the constructor will be the presence of other super statement, and must be the first line in the constructor.

9. inherited polymorphisms


Fourth, overloading and rewrite

  1. Overload (overload) rules

    1. You must have a different list of parameters
    2. Regardless of the return value
    3. You can have different access modifiers
    4. You can throw different exceptions
    5. Or the method can be overridden in a subclass of a class.
    6. Present in the parent class, subclass, and in the same
  2. Overloaded meaning

    1. Be regarded as an alternative if else, even less than if else to write the code, parameter passing is more clear, it is conducive to team development
    2. Different input and output the same time, the general might use overloaded
  3. Rewriting (override) rules

    1. Method name, arguments, return values ​​for the same
    2. Subclass method can not be reduced access to the parent class method
    3. Subclass method can not throw more than the parent class method exceptions (but subclass method can not throw an exception)
    4. It exists between the parent and child classes
    5. Is defined as the final method can not be rewritten.
  4. Rewrite the meaning of

    1. Subclasses of the parent class to be extended
  5. The difference between overloading and rewrite
    rewrite Override and overload Overload · 1. Java methods are different manifestations of polymorphism. Override rewriting is a manifestation of polymorphism between the parent class and subclass Overload Overload is a polymorphism of a performance

    2. Method overloading refers to the same class, the same method for a plurality of names, a list of their argument. The method of rewriting refer to the same sub-class and the parent class method, the relationship between the two classes


V. rewrite toString method

  1. Why should override the toString method
    1. toString () method is a method in which the Object class, which returns a String data type: hex class name + @ + hash value of the
    reference object 2. Direct Print, calls default toString () method
Student stu = new Student("Jack",18);
    System.out.println(stu);
  1. How to override toString method
    1. shortcut Alt + Shift + S, click Override, select toString () method
    2. Student class which press Alt + Shift + S, click the Generate toString () method
public String toString() {
return super.toString();
}

//法1
public String toString() {
return "我的名字是" + name + "年龄是" + age;
}

//法2
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}


Six polymorphic

1, the basic concept

  1. Specific reference variable type defined in the program pointed to by the reference variable and a method call issued when programming is not determined, but is determined only during the program run
  2. Polymorphism refers to the same act, with many different forms

2, polymorphic premise

  1. Inherit or implement
  2. Method overrides
  3. Parent class reference subclass object (upcast)

3, multi-state calls

  1. When using polymorphic method calls, first check whether there are in the parent class method, if the parent class does not compile errors.

  2. If there is the parent class, the subclass is executed to rewrite the parent class (i.e., upward transition)

    1. Why do some subclass does not override this method, but still perform, because although the subclass did not write it, but he inherits the parent class this method, but there is no clear written, so the actual call is inherited from the parent subclasses methods of the class, super (); but did not write
class  Father{
    public void fu(){
        System.out.println("Father");
    }
}
class Sun extends Father{
    public void sun(){
        System.out.println("sun");
    }
}
public class Test {
    public static void main(String[] args) {
        Father f=new Sun();
        f.fu();
    }
//运行结果:Father
}

4, multi-state dynamic and static binding

  1. Binding refers to the class and call a method where the method (method body) associate, is a method to call another method
  2. Static Binding (early binding), is compiled on the binding, Java methods only final, static, private (not inherited) and construction methods are statically bound , using a type of information
  3. Dynamic binding (late binding), according to the type of specific binding objects at run time, using the object information

5, downcast and upcast

  1. UpcastFather father = new Son();
  2. DowncastSon s =(Son) father;
  3. Why should there be downcast when using polymorphic method calls, first check whether there is the parent class method, if not the compiler error, that is polymorphic and can not be called a subclass of parent class has no way, if you want to call subclass unique method, do downcast

6, instanceof determines the data type

  1. Concept: Returns If the variable part of the data type true, false otherwise
  2. Role: both this subclass inherits the parent class method of the same name, if not determine, the type of data being given

Seven, generic (Generic)

  1. Basic concepts
    1. Can be set to store any object, the object is automatically converted into a collection type, so this takes out an object to be typecast
    2. However, if stored in the collection, such as a variety of objects, the program will take place at runtime Java.lang.ClassCastException unusual, because in fact the same type of object storage Collection only
  2. Key words
    1. E (Element) in the set, because the collection is stored in the element
    2. T (Type) T represents the specified type when you call
    3. K (key 键)
    4. V (Value Value)
    5. N (Number) Value Type
    6. ? Expressed uncertainty Java type, usually used when Wild

Eight, processes, threads, multi-threaded

  1. Process concept
    1. Process is a program running on the system, the program once the process is running,
    2. Process is an independent entity resource allocation system, each process has a separate address space
    3. If you want to access a resource process to another process, the need to use inter-process communication
    4. A process can have multiple threads
  2. Multithreading Concepts
    1. Each thread uses the process stack space to which it belongs
    2. A thread is a physical process, is a process of implementation of the Road King
    3. A thread is a specific process of the implementation of the Road King, when a thread modifies the process of resources, his brother thread can immediately see this change
  3. Multithreading
    1. Multithreading refers to the production of multiple threads in a process running
  4. Parallel and Concurrent
    1. Parallel: a plurality of CPU instances or multiple machines simultaneously performed for a processing logic
    2. Concurrency: The CPU scheduling algorithm, allowing users to perform simultaneous looked like, but the CPU is the use of a public resource level law
  5. There will be a problem
  6. Thread safety: This means that the thread scheduling order will affect the final result
  7. Sync: refers to artificially control and scheduling, to ensure that multi-threaded access to shared resources called thread-safe, to ensure accurate results
  8. Deadlock
Published 23 original articles · won praise 2 · Views 1028

Guess you like

Origin blog.csdn.net/weixin_46101839/article/details/104484639