String, StringBuffer, StringBuilder

  • To manipulate a small amount of data ->String
  • Single thread manipulation of large amounts of data under string buffer ->StringBuilder
  • Multithreaded manipulation of large amounts of data in string buffers ->StringBuffer
  • Variability: The final keyword is used in the String classprivate final char value[], soStrings are immutable. AbstractStringBuilder and StringBuffer both inherit from the AbstractStringBuilder class. AbstractStringBuiler class AbstractStringBuiler also uses character arrays to store stringschar[] valur, but the final keyword is not used, soStringBuilder and StringBuffer are mutable.
  • Thread-safe: Objects in strings are immutable and can be understood as constants, so strings are thread-safe. AbstractStringBuilder AbstractStringBuilder is a common parent of StringBuilder and StringBuffer. It defines some basic string operations, such as append, INSERT, indexOf, and other public methods. StringBuffer places a synchronization lock on a method or on a called method, so it is thread-safe. StringBuilder does not lock methods synchronously, so StringBuilder is thread-unsafe.
  • Performance: Every time a String type is changed, a new String is generated and a pointer is pointed to the new String. A StringBuffer operates on the StringBuffer object itself each time, rather than generating new objects and changing object references. Using StringBuilder gives a 10%-15% improvement over StringBuffer in the same situation, but at the risk of being thread unsafe.

2. Packing and unpacking

  • Boxing: Base types are wrapped in their corresponding reference types
  • Unpacking: Converts wrapper type to base data type

The function of the no-parameter constructor

If the program does not call the superclass-specific constructor with super() before executing the subclass constructor, it will call the constructor with no arguments in the superclass. Therefore, if only the constructor with arguments is defined in the parent class, and no super() is used in the subclass constructor to call the specific constructor in the parent class, an error will occur at compile time. And note that if we don’t define any constructors, the program will automatically define a constructor for us that takes no arguments by default. If only the parameter constructor is defined, the program will not help us define the no-parameter constructor. So the most conservative approach is to manually define a constructor with no parameters.

Java package, Javax package

Originally the package required by the JavaAPI was a Java package, and the Javax package was simply used to extend the API package. Over time, javax packages have expanded to become part of the JavaAPI. However, direct migration was cumbersome and broke much of the original code, so the decision was made to make the Javax package part of the standard API.

Interface, abstract class

  • The default interface method is public, all methods can not have implementation in the interface (java8 interface methods can be implemented by default), abstract classes can have non-abstract methods, abstract methods must only be declared, subclass implementation
  • Instance variables in interfaces are final by default, but not necessarily in abstract classes
  • A class can implement multiple interfaces, but can inherit at most one abstract class
  • A class that implements an interface implements all the methods of the interface, not an abstract class
  • Interfaces cannot be instantiated with new, but can be declared, but must reference an object that implements the interface
  • Abstract is the abstraction of class, is a template design; An interface is an abstraction of behavior and a specification of behavior
  • Note: in java8, interfaces can define static methods that can be called directly with the interface name, but implementation classes and implementations cannot be called. If two interfaces are implemented at the same time and the same default methods are defined in the interfaces, they must be overridden.

6. Member variables, local variables

  • Syntax: A member variable belongs to a class, and a local variable belongs to a method (in which the parameters of the or method are defined). Member variables can be modified by public, private, and static modifiers, while local variables cannot be modified by access control modifiers and static modifiers. However, both can be modified by final
  • Storage in memory: Local variables, if primitive data type, are stored directly on the stack, if wrapper type, for exampleInteger i = new Integer (12);, will store the object in the heap, the object’s reference in the stack. Member variables, members of a class that vary from object to object, base data types and reference data types are stored in this object, stored in the heap as a whole. The method of the class is shared by all objects, the method is the existence of the method area, only when the call will be pushed, not when the memory is occupied.
  • Lifetime: A member variable is a part of an object that exists with the creation of the object; Local variables disappear automatically after a method is called
  • No initialization: Member variables are assigned to the default values of their types (except for variables modified by final); Local variables are not automatically assigned

What operator is used to create an object? Differences between object entities and object references?

Using the new operator, new creates the object instance (which is in heap memory) and the object reference points to the object instance (which is in stack memory). Think of object entities as balloons and object references as strings: a string can be tied to either a balloon or not; A balloon can be tied with n strings. Conclusion: An object reference can refer to 0 /1 object entities; An object entity can have n object references to it.

8, the characteristics of the construction method

  • Name is the same as the class name
  • There is no return value, but it cannot be declared with void
  • It is automatically executed when the object is generated, and no manual invocation is required

Static method, instance method

Class names can be used when static methods are invoked externally. Method name, or object name. Method name. Instance methods can only pass object names. Method name. When a static method accesses a member of this class, it only allows access to static variables and methods, but does not allow access to instance variables and methods. Instance methods have no such restrictions

10. Final keyword

  • Variables: For a final variable, if it is of a basic data type, its value cannot be changed once initialized; If a variable is a reference type, it cannot be made to point to another object after it is initialized.
  • Classes: When you modify a class with final, it indicates that the class cannot be inherited. All member methods ina final class are implicitly specified as final methods.
  • Method: The first reason is to lock the method in case any inherited class changes its meaning and cannot override it. The second reason is efficiency. In earlier Versions of Java implementations, final methods were converted to inline calls. But if the method is too large, you may not see any performance gains from inline calls (these optimizations with final methods are no longer required in current Java versions). All private methods ina class are implicitly specified as final.