catalogue

  • 2.0.0.1 Four features of Object-oriented programming and what do they mean? What are encapsulation, inheritance and polymorphism? Why encapsulate? Why single inheritance and not multiple inheritance?
  • 2.0.0.2 What is the difference between overloading and overwriting? What is the difference between overloading and overwriting binding mechanisms? Can a static method of a parent class be overridden by a subclass? Rewriting is dynamic binding, how to understand the mechanism?
  • 2.0.0.3 What is binding? How are static and dynamic bindings different? How does dynamic binding compilation work? How does dynamic binding work?
  • 2.0.0.4 What is the difference between interfaces and Abstract classes? What is the meaning of interfaces? What is the meaning of abstract classes? How do I choose abstract classes and interfaces?
  • 2.0.0.5 Why must external variables of inner class calls be final? How do local variables affect the garbage collection mechanism?
  • 2.0.0.7 What is polymorphism? What are the ways to implement polymorphism? What are the disadvantages of polymorphism? What are the requirements for Java to implement polymorphism? How does polymorphism work?
  • 2.0.0.9 What is the difference between static variables and member variables? What are the code blocks? Which comes first, the construct code block or the constructor?
  • 2.0.0.8 Abstract the service methods of different object classification, disintegrate the tightly coupled relationship of business logic, and realize the isolation of code to ensure convenient extension?
  • 2.0.1.0 What are the characteristics of abstract classes? What is the difference between an abstract class and an ordinary class? Can abstract classes be new? What could go wrong?
  • 2.0.1.1 What are inner classes and what are they? What is the difference between a static inner class and a non-static inner class? What are the main functions of inner class effects? Does an inner class relate to an outer class?

Good news

  • Summary of blog notes [October 2015 to present], including Java basic and in-depth knowledge points, Android technology blog, Python learning notes, etc., including the summary of bugs encountered in daily development, of course, I also collected a lot of interview questions in my spare time, updated, maintained and corrected for a long time, and continued to improve… Open source files are in Markdown format! Also open source life blog, since 2012, accumulated a total of 500 articles [nearly 1 million words], will be published on the Internet, reprint please indicate the source, thank you!
  • Link address:Github.com/yangchong21…
  • If you feel good, you can star, thank you! Of course, also welcome to put forward suggestions, everything starts from small, quantitative change causes qualitative change! All blogs will be open source to GitHub!

2.0.0.1 Four features of Object-oriented programming and what do they mean? What are encapsulation, inheritance and polymorphism? Why encapsulate? Why single inheritance and not multiple inheritance?

  • encapsulation
    • To wrap the properties and behavior of something into an object, forming an indivisible and independent entity.
    • Encapsulation privatized the properties of an object while providing methods for properties that can be accessed by the outside world. If the properties do not want to be accessed by the outside world, we do not need to provide methods to access them. But if a class has no methods to access from the outside world, then that class is meaningless.
  • inheritance
    • Inheritance is the technique of using the definition of an existing class as a basis to create a new class. The definition of a new class can add new data or new functions, or use the functions of the parent class, but can not selectively inherit the parent class. Using inheritance makes it very easy to reuse old code.
    • Pay attention toTech blog summary
      • Subclasses have properties and methods that are not private from their parent class.
      • Subclasses can have their own attributes and methods, that is, they can extend their parent class.
      • Subclasses can implement the methods of their parent class in their own way.
  • polymorphism
    • So-called polymorphism is refers to the procedures defined in the reference variable is pointing to the specific type and referenced by the variable from method calls are not sure when programming, but during the program is run to determine, that is, a reference variable STLL can point to which class instance object, the reference variable from method calls the method to realize exactly is which class, It must be decided during the running of the program.
    • Polymorphism can be implemented in Java in two forms: inheritance (overwriting the same method by multiple subclasses) and interfaces (implementing the interface and overwriting the same method in the interface).
  • abstract
    • Generalize things in the real world and abstract them into entities that make sense in the virtual world of computers
  • Why single inheritance and not multiple inheritance?
    • If there is multiple inheritance, the result of subclass calls will be ambiguous when there are duplicate properties or methods in multiple parent classes, so single inheritance is used.
    • Although multiple inheritance can make subclasses have the characteristics of multiple parent classes at the same time, its disadvantages are also very significant, mainly in two aspects:
      • If a subclass inherits an instance variable of the same name from more than one parent class, the subclass will have an ambiguity when referring to the variable and cannot determine which parent class variable should be used.
      • If you have the same method in multiple parent classes that a subclass inherits, and whether the method is overridden in the subclass or not, then the method will be ambiguous when calling, and you cannot determine which parent class method should be called.
    • Java is optimized from the C++ language, and C++ is also object-oriented, why can it inherit more? First of all, C++ was introduced in 1983 on the basis of C, and Java was introduced in 1995. Second, when C++ is designed, it often falls into the trap of multiple inheritance. Although it provides a solution, the Java language does away with multiple inheritance in C++ for the sake of simplicity, which also makes programs more secure.
  • Why multiple implementations?Tech blog summary
    • By implementing the interface to extend the functionality of the class, if the implementation of multiple interfaces have duplicate methods, it does not matter, because the implementation class must override the interface method, so call the implementation class overridden method.

2.0.0.2 What is the difference between overloading and overwriting? What is the difference between overloading and overwriting binding mechanisms? Can a static method of a parent class be overridden by a subclass? Rewriting is dynamic binding, how to understand the mechanism?

  • overloading
    • The method name must be the same, the parameter type must be different, the number of parameters must be different, and the method return value and access modifier can be different.
  • rewrite
    • Overrides the method by which a subclass overrides its parent
    • In the parent class, the method name and parameter list must be the same, the return value range must be smaller than or equal to the parent class, the exception range must be smaller than or equal to the parent class, and the access modifier range must be larger than or equal to the parent class. If the superclass method access modifier is private, the subclass cannot override the method.
  • What is the difference between overloading and overwriting binding mechanisms?Tech blog summary
    • Overloading: in-class polymorphism, static binding (which method is executed is known at compile time), methods with the same name and different parameters
    • Rewrite: Inter-class polymorphism, dynamic binding mechanism (determined at run time), instance method, two small and two large (method signature is the same, the method of the subclass throws exceptions, return value range is not greater than the corresponding method of the parent class, the method visibility of the subclass is not less than the corresponding method of the parent class) method signature is the same, The range of exceptions and return values thrown by the methods of a subclass is no greater than that of the corresponding methods of the parent class, and the visibility of the methods of a subclass is no less than that of the corresponding methods of the parent class
  • Can a static method of a parent class be overridden by a subclass?
    • A static method of a parent class cannot be overridden by a subclass. In fact, overwriting applies only to instance methods, not to static methods. For static methods like this, we should call it hidden.
    • Tech blog summary
    • Java static methods can be formally rewritten, but are not inherently Java rewritten. Static methods are class-specific, not implementation-specific. The static method of the corresponding class is referenced (it is not required to declare static, but can be directly referenced). And static methods are not bound late; they are bound at compile time. In other words, this method does not make polymorphic judgments, only about the declared class.
  • Rewriting is dynamic binding, how to understand the mechanism?
    • In the process of running the program, the method can be determined according to the specific instance object.
    • Dynamic binding is one of the important factors, polymorphism realized it by method table: each class is loaded into the virtual machine, the method of preservation metadata, which includes a method called table method (table), recorded the method of the class definition in the table of Pointers, each table item points to a specific method code. If the class overrides a method in the parent class, the corresponding entry points to the new code implementation. Methods inherited from a parent class precede methods defined by subclasses.

2.0.0.3 What is binding? How are static and dynamic bindings different? How does dynamic binding compilation work? How does dynamic binding work?

  • What is binding?
    • Associating a method with its class/object is called method binding. Bindings are divided into static (early binding) and dynamic (late binding).
  • How are static and dynamic bindings different?
    • Static binding (pre-binding) means:
      • The method is known to belong to the class before the program runs, and at compile time you can connect to the class to locate the method.
      • In Java, final, private, and static modified methods and constructors are statically bound, and the contents of the method are known without the need for an instance object to run the program.
    • Dynamic binding (late binding) refers to:
      • In the process of running the program, the method can be determined according to the specific instance object.
      • Dynamic binding is one of the important factors, polymorphism realized it by method table: each class is loaded into the virtual machine, the method of preservation metadata, which includes a method called table method (table), recorded the method of the class definition in the table of Pointers, each table item points to a specific method code. If the class overrides a method in the parent class, the corresponding entry points to the new code implementation. Methods inherited from a parent class precede methods defined by subclasses.
  • Dynamic binding compiler principlesTech blog summary
    • We assume Father ft=new Son(); ft.say(); Son overwrites say() from Father.
    • Compilation: We know that when we transition up, we use a superclass reference to execute a subclass object, and we can use a superclass reference to call a method of the same name that was overridden in the subclass. But you can’t call a new method in a subclass. Why?
    • Because at compile time, the compiler declares the type of the object (that is, the type that refers to itself) and looks for matching methods in the method table of that type in the method area (best match method: the closest argument type is called), and if there are any, the compiler passes. Father = Father; Father = Father; Father = Father;
    • Compilation stage is to ensure the existence of the method, to ensure that the program can run smoothly and safely.
  • How does dynamic binding work?
    • Ft.say () calls Father’s say(); ft.say() calls Father’s say(); No, this is the dynamic binding mechanism in action.
    • The above compilation phase looks for methods in the method table where the object type is declared, just to safely pass compilation (and also to verify that the method exists). Father ft=new Son(); This statement creates a Son instance object, and when the method is called by ft. Say (), the JVM pushes the Son object onto the operand stack and uses it to make the call. The process of making a method call with an instance object is dynamic binding: the method table of the instance object is searched according to its type, and a matching method is found to call. We know that if a subclass overrides a parent class’s method, the same entry in the method table points to the subclass’s method code. If not overridden, it is saved in the subclass method table in the order of the method table in the parent class. Therefore, dynamically binding methods that look up methods based on the type of the object in the method table are bound to match. This also explains why an upcast parent reference cannot call a method added by a subclass: the existence of the method must be checked in the parent method table first, which is dangerous if checked at runtime — it may not exist in the subclass either. Tech blog summary
  • Distinguish between the two
    • When the JVM is running, the program will load the class type information, static properties and methods, final constants and other metadata into the method area. These are known when the class is loaded, and can be accessed without object creation. Dynamically bound content that needs to be retrieved based on the type of instance object in the heap when the object is created.

2.0.0.3 What is the difference between interfaces and Abstract classes? What is the meaning of interfaces? What is the meaning of abstract classes? How do I choose abstract classes and interfaces?

  • What is the difference between interfaces and abstract classes
    • The default method of an interface is public, all methods can have no implementation in the interface (Java 8 starts with interface methods having default implementations), and abstract classes can have non-abstract methods
    • Instance variables in interfaces are final by default, but not necessarily in abstract classes
    • A class can implement multiple interfaces, but only one abstract class at most
    • A class that implements an interface must implement all the methods of the interface, not necessarily an abstract class
    • Interfaces can not be instantiated with new, but can be declared, but must reference an object to implement the interface from the design level, abstraction is the abstraction of class, is a template design, interface is the abstraction of behavior, is a behavior specification.
  • What does an interface do
    • Tech blog summary
    • 1. Importance: Abstract class and Interface are two mechanisms in the Java language that support abstract class definitions. It is these two mechanisms that give Java its powerful object-oriented capabilities.
    • 2, simple, normative: if a project is large, it needs to be a can clarify all business architects to define some of the main interface, the interface you tell developers need to implement the business not only, but also will restrict the naming conventions (to prevent some developers call the lead to other programmers cannot see).
    • 3. Maintenance/extensibility: For example, if you want to create a sketchpad application, there is a panel class that is responsible for drawing, and then you define this class. But in the near future, you suddenly find that the class does not satisfy you, and then you have to redesign the class, or worse, you may have to abandon the class, and then there may be references to it somewhere else, which is very troublesome to modify.
    • If you first define an interface, put the drawing function in the interface, and then implement the interface when defining the class, then you just use the interface to reference the class that implements it, and then you just refer to another class, so that you can achieve easy maintenance and extension.
    • 4. Security and tightness: Interface is an important means to achieve loose coupling of software. It describes all external services of the system without involving any specific implementation details. In this way, it is more secure and strict (general software service providers consider more).
  • What is the meaning of abstract classes
    • 1. Because an abstract class cannot instantiate an object, it must have a subclass to implement it before it can be used. This makes it easier to maintain code and programs by abstracting components that have the same properties and methods.
    • 2. When a similar component is generated, the properties and methods of the abstract class can be obtained simply by implementing the abstract class.
  • How do I choose abstract classes and interfaces?
    • Use interface:Tech blog summary
      • The compareTo() method of the Compareable interface can be implemented by all unrelated classes.
      • Multiple inheritance is required.
    • Using abstract classes:
      • You need to share code in several related classes.
      • You need to be able to control the access of inherited members, not all of them public.
      • You need to inherit non-static and nonstatic fields.

2.0.0.4 What are inner classes and what are they? What is the difference between a static inner class and a non-static inner class? What are the main functions of inner class effects? Does an inner class relate to an outer class?

  • What is an inner class
    • An inner class is a class defined inside another class. It is hidden in the external class, encapsulation is stronger, does not allow other classes outside the external class to access it; But it has direct access to the members of the external class.
  • What are the inner classes
    • Member inner class: The member inner class is a member of the enclosing class and is attached to the enclosing class. Therefore, the inner class object can be created only if the enclosing class object is created first. For this reason, member inner classes cannot contain static variables and methods;
    • Static inner class: a static inner class is an inner class that is modified to be static. The inner class object does not depend on the outer class object. That is, we can create an inner class object directly, but it can access only the static members and methods of the outer class.
    • Local inner classes: A local inner class is compiled just like a member inner class, except that its scope has been changed. It can only be used in methods and properties, and is invalidated outside the methods and properties. Tech blog summary
    • Anonymous inner classes: To define an anonymous inner class, the inner class must inherit a class or implement an interface in the form of a new parent or interface (){define the contents of a subclass (such as functions)}. That is, the anonymous inner class ends up giving us the object of an anonymous subclass.
  • The differences between static inner classes and non-static inner classes are:
    • A static inner class is an inner class that is declared static and does not rely on external class instantiation. Non-static inner classes need to be generated indirectly by generating outer classes.
    • A static inner class can access only the static member variables and static methods of an external class, whereas a non-static inner class can access the members used by the external class because it holds a reference to the external class
  • What are the main functions of inner class effects?
    • The inner class function mainly realizes the function to hide, reduces the memory overhead, enhances the program to run the speed
  • Inner class and outer class contact:
    • Inner class can access external class all of the methods and properties, if inside class and outside class members have the same method and the member attribute, a member of the inner class method calls to take precedence over external class is the priority of the inner class is higher (limited to class, in the main method, the inner class object cannot access external members of a class method and member attribute). An outer class can access only static constants of an inner class or member properties and methods of an inner class by creating an inner class

2.0.0.5 Why must local variables of inner class calls be final? How do local variables affect the garbage collection mechanism?

  • Why must the outer variables of inner class calls be final?
    • Simple answer: On the one hand, because the life cycle of a local variable ina method is short, the variable must be destroyed once the method ends. In order to ensure that the internal class can find the external local variable, a reference to the external variable can be obtained through the final keyword. On the other hand, the final keyword does not change the value of the variable internally, protecting data consistency.
    • More specifically: because of the life cycle. A local variable ina method that is released when the method ends, and final ensures that the variable always refers to an object. First, the inner class and the outer class are actually at the same level. The inner class is not defined in a method and then destroyed as the method completes execution. The problem is that if a variable in an outer class’s method does not define final, then the local variable will be GC when the outer class’s method completes execution, while the inner class’s method has not completed execution and the reference to the outer variable will not be found. If final is defined, Java makes a copy of the variable as a member variable built into the inner class, so that since the value modified by final never changes, the memory region to which the variable points does not change.
    • To solve the problem of inconsistency between the life cycle of a local variable and the life cycle of an object of a local inner class
    • Note: In Java 1.8, you can do without the final modifier, but don’t be misled because if you don’t use the final modifier, modifying its value in an anonymous inner class will still result ina compilation error. Because Java 1.8 actually makes it final automatically
  • How do local variables affect the garbage collection mechanism?Tech blog summary
    • Conclusion: Variables in the local variable scale are important garbage collection root nodes. Objects referenced directly or indirectly by variables in the local variable scale will not be collected.

2.0.0.7 What is polymorphism? What are the ways to implement polymorphism? What are the disadvantages of polymorphism? What are the requirements for Java to implement polymorphism? How does polymorphism work?

  • What is polymorphism?
    • Polymorphism refers to the process defined in the reference points to a specific type and referenced by the variable from method calls are not sure when programming, but during the program is run to determine, that is, a reference variable STLL can point to which class instance object, the reference variable from method calls the method to realize exactly is which class, must can decide during the program is running. Because the program is running to identify a specific class, so, don’t need to modify the source code, can make reference variables bound to the different class implements, leading to the reference call specific methods change, namely do not modify the program code can change the program of the specific binding in the runtime code, let the program can select multiple running state, this is polymorphism.
  • Polymorphic implementation conditions?Tech blog summary
    • There are three necessary conditions for Java to implement polymorphism: inheritance, rewriting, and upward transition.
    • Inheritance: In polymorphism, there must be a subclass and a parent class that have an inheritance relationship.
    • Override: A subclass redefines methods in its parent class that are called when those methods are called.
    • Upward transition: In polymorphism, a reference to a subclass is assigned to a superclass object so that the reference has the ability to call methods of the parent class and methods of the subclass.
  • What are the ways to implement polymorphism?
    • Polymorphism: Polymorphism is when the same message causes different classes to respond differently.
    • The first implementation: polymorphisms based on inheritance
      • The implementation mechanism based on inheritance is mainly manifested in the rewriting of some methods by the parent class and one or more subclasses that inherit the parent class. Multiple subclasses can show different behaviors when rewriting the same method. Polymorphism is when different objects can perform the same behavior, but they all need to implement it in their own way, thanks to the upward transition.
      Public class MainJava {public static void main(String[] args) {Wine[] wines = new Wine[2]; // Define two subclasses Test1test1 = new Test1();
              Test2 test2 = new Test2(); Wine win e = new Wine(); // The parent class references the subclass object wines[0] =test1;
              wines[1] = test2;
              for(int i = 0 ; i < 2 ; i++){
                  System.out.println(wines[i].toString() + "--" + wines[i].drink());
              }
              System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
              System.out.println(test1.toString() + "--" + test1.drink());
              System.out.println(test2.toString() + "--" + test2.drink());
          }
          public static class Wine {
              private String name;
              public String getName() {
                  return name;
              }
              public void setName(String name) {
                  this.name = name;
              }
              public String drink() {return "Drink it." + getName();
              }
              public String toString() {return null;
              }
          }
      
          public static class Test1 extends Wine{
              public Test1() {setName("Test1");
              }
              public String drink() {return "Drink it." + getName();
              }
              public String toString() {return "Wine : " + getName();
              }
          }
      
          public static class Test2 extends Wine{
              public Test2() {setName("Test2");
              }
              public String drink() {return "Drink it." + getName();
              }
              public String toString() {return "Wine : "+ getName(); }}}Copy the code
    • The second way to implement polymorphism is based on interface implementation
      • Inheritance is represented by overriding several different subclasses of the same method in the parent class, and thus by implementing the interface and overriding several different classes of the same method in the interface.
      • In interface polymorphism, a reference to an interface must be an instance program that specifies a class that implements the interface and, at run time, executes the corresponding method according to the actual type of the object reference.
      • Inheritance is single inheritance and can only provide a consistent service interface for a set of related classes. But the interface can be multiple inheritance and multiple implementation, it can use a group of related or unrelated interface combination and expansion, can provide a consistent external service interface. So it has more flexibility than inheritance.
  • What are the disadvantages of polymorphism?Tech blog summary
    • Subclass-specific properties and behaviors cannot be used without transformation
    class Demo_SuperMan { public static void main(String[]args){ Person p=new SuperMan(); // A parent class reference points to a subclass object. // Superclass references refer to subclass objects, which are upcast system.out.println (p.name); p.Tsy(); //p.Fly(); SuperMan sm=(SuperMan)p; // Scroll down to see the contents of the entire object sm.fly (); } } class Person{ String name="John";
        public void Tsy(){
            System.out.println("Tsy");
        }
    }
     
    class SuperMan extends Person{
        String name="SuperName";
        @Override
        public void Tsy(){
            System.out.println("Subclasses Tsy");
        }
     
        public void Fly(){
            System.out.println("Fly out and save people."); }}Copy the code

2.0.0.8 Abstract the service methods of different object classification, disintegrate the tightly coupled relationship of business logic, and realize the isolation of code to ensure convenient extension?

  • Take a look at the following code, adapted from a great company’s product code. How do you think object-oriented design principles can be improved?
    public class VIPCenter {
        void serviceVIP(T extend User user>) {
         if(user instanceof SlumDogVIP) {//do somthing
          } else if(user instanceof RealVIP) {
            // do somthing
          }
          // ...
    }
    Copy the code
    • One problem with this code is that the business logic is centralized, and when a new type of user comes along, for example, big data discovers that we are fat sheep and needs to be harvested, this requires direct modification of the service method code implementation, which may accidentally affect an unrelated type of user logic.
    • Using the switch principle, you can try the following code. Abstract the service methods of different object classes, separate the tightly coupled relationship of business logic, and realize the isolation of code to ensure convenient extension. Tech blog summary
    public class VIPCenter { private Map<User.TYPE, ServiceProvider> providers; Void serviceVIP(T extend User User) {provider.get (user.getType()).service(User); } } interface ServiceProvider{ void service(T extend User user) ; } class SlumDogVIPServiceProvider implements ServiceProvider{ void service(T extend User user){ //do somthing
        }
    }
    
    class RealVIPServiceProvider implements ServiceProvider{
        void service(T extend User user) {
            // do something
        }
    }
    Copy the code

2.0.0.9 What is the difference between static variables and member variables? What are the code blocks? Which comes first, the construct code block or the constructor?

  • The difference between static and member variables
    • A: They belong to different categories
      • Static variables are classes, so they are also called class variables
      • Member variables belong to objects, so they are also called instance variables (object variables)
    • B: Different locations in memory
      • Static variables are stored in the static section of the method area
      • Member variables are stored in heap memory
    • C: Memory occurs at different timesTech blog summary
      • Static variables are loaded as the class is loaded and disappear as the class disappears
      • Member variables exist as the object is created and disappear as the object disappears
    • D: Different calls
      • Static variables can be called either by class name or by object
      • Member variables can only be called by object name
  • What are the code blocks
    • A: Code block overview
      • In Java, code enclosed in {} is called a code block.
    • B: Code block classification
      • Depending on their location and declaration, they can be divided into local code blocks, construction code blocks, static code blocks, and synchronous code blocks.
    • C: Application of common code blocks
      • A: Local code blocks
        • Appear in a method; Limited variable life cycle, early release, improve memory utilization
      • B: Construct code blocks
        • Occurs outside a method in a class; The same code in multiple constructor methods is stored together, executed each time the constructor is called, and executed before the constructor
      • C: Static code block
        • Occurs outside a method in a class with the static modifier
        • Occurs outside a method in a class with the static modifier; It is used to initialize a class and is executed at load time and only once.
  • Which comes first, the construct code block or the constructor?

2.0.1.0 What are the characteristics of abstract classes? What is the difference between an abstract class and an ordinary class? Can abstract classes be new? What could go wrong?

  • What are the characteristics of abstract classesTech blog summary
    • Both abstract classes and abstract methods are declared using the abstract keyword. Abstract classes typically contain abstract methods, which must reside in the abstract class.
  • What’s the difference between an abstract class and an ordinary class
    • The biggest difference between an abstract class and a normal class is that an abstract class cannot be instantiated. It must inherit from the abstract class to instantiate its subclasses.
    public abstract class AbstractClassExample {
        protected int x;
        private int y;
        public abstract void func1();
    
        public void func2() {
            System.out.println("func2");
        }
    }
    
    public class AbstractExtendClassExample extends AbstractClassExample {
        @Override
        public void func1() {
            System.out.println("func1"); }}Copy the code
  • Can abstract classes be new? What could go wrong?
    • Note that abstract classes cannot be instantiated, i.e. cannot be new!
    • If new is required, it is prompted

2.0.1.1 What Are Java Data Types? What is value passing? What is passing by reference? How to understand value passing and reference passing, and how are they different?

  • What are Java data types?Tech blog summary
    • There are two types of data types in Java: basic types and reference types.
      • 1. Primitive variables hold the original value, so the variable is the data itself.
        • Common basic types: byte, short, int, long, char, and float, double, Boolean, returnAddress.
      • 2. Reference variables hold reference values. The so-called reference value is the “first address value” of the memory space in which the object is located.
        • Common reference types: class types, interface types, and arrays.
    • Primitive types
      • Primitive types include Boolean types and numeric types. Numeric types are further divided into integer types and floating-point types. There are five types of integers: byte short int long char(char is essentially a special int). Floating point types have floats and doubles.
    • Reference types
      • ① interface ② class ③ array
  • What is value passing?
    • During a method call, the argument passes its actual value to the parameter. This is done by passing a copy of the value of the argument to the function so that operations on the value (the parameter’s value) in the function do not affect the value of the argument. Because of direct replication, this approach can be particularly inefficient when passing large amounts of data.
    • The String class, for example, is designed to be immutable, so each assignment recreates a new object, hence value passing!
  • What is passing by reference?Tech blog summary
    • Reference passing makes up for the deficiency of value passing. If a large amount of data is passed, it will take up a lot of memory space.
    • Passing by reference is passing the address of an object. The function receives the initial address of the original value.
    • During the execution of a method, the parameters and arguments have the same content and point to the same memory address, which means that the operation is actually the source data, so the execution of the method will affect the actual object.
  • How to understand value passing and reference passing, and how are they different?
    • Take a look at the code example below
      private void test1(){
          Demo demo = new Demo();
          demo.change(demo.str, demo.ch);
          Log.d("yc---",demo.str);
          Log.d("yc---", Arrays.toString(demo.ch)); Yc --: hello //yc-- : [c, b]} public class Demo {String STR = new String("hello");
          char[] ch = {'a'.'b'};
          public void change(String str, char[] ch) {
              str = "ok";
              ch[0] = 'c'; }}Copy the code
    • Case process analysis
      • Allocates space for objects
      • Execute the change() method
        • Arguments (black) and parameters (red) point to the following before execution:
      • The final print
        • String is immutable and is passed by value. Ch [] is passed by reference, so STR = “ok” in this method is equivalent to creating an object without changing the value of the argument STR. Array is passed by reference and is changed directly.
  • Through the above analysis, we can draw the following conclusions:Tech blog summary
    • The base data type is passed as a value. Changes to the parameter do not affect the argument.
    • Reference types pass references, and parameters and arguments point to the same memory address (the same object), so changes to parameters affect the actual object.
    • String, Integer, Double, and other immutable types are special processing that can be interpreted as transferring values. The last operation does not modify the argument object.
  • How to understand the passing by value of reference types?
    • The reference type is passed by value, passing the address of the object. We just get the address of the element, we don’t copy the element. Array, for example, is passed by reference. If it is passed by value, then in method call assignment, passing a copy of the value of the argument to the function will be very inefficient.

The other is introduced

01. About blog summary links

  • 1. Tech blog round-up
  • 2. Open source project summary
  • 3. Life Blog Summary
  • 4. Himalayan audio summary
  • 5. Other summaries

02. About my blog

  • My personal website: www.yczbj.org, www.ycbjie.cn
  • Github:github.com/yangchong21…
  • Zhihu: www.zhihu.com/people/yang…
  • Jane: www.jianshu.com/u/b7b2c6ed9…
  • csdn:my.csdn.net/m0_37700275
  • The Himalayan listening: www.ximalaya.com/zhubo/71989…
  • Source: China my.oschina.net/zbj1618/blo…
  • Soak in the days of online: www.jcodecraeer.com/member/cont.
  • Email address: [email protected]
  • Blog: ali cloud yq.aliyun.com/users/artic… 239.headeruserinfo.3.dT4bcV
  • Segmentfault headline: segmentfault.com/u/xiangjian…
  • The Denver nuggets: juejin. Cn/user / 197877…