1. JAVA [Basic]

What is object-oriented?

Object-oriented is a kind of thought, everything in the world can be regarded as an object, here only discusses object-oriented programming (OOP), JAVA is a computer programming language that supports high concurrency, class-based and object-oriented. Object-oriented software development has the following advantages:

  1. Modular code development, easier to maintain and modify
  2. Enhanced code reusability, reliability and flexibility
  3. Improved code readability

What are the characteristics of object-oriented?

In general, there are four categories: encapsulation, inheritance, polymorphism, and abstraction

  1. Encapsulation:
    Encapsulation refers to the ability to hide internal characteristics and behaviors for an object. The object provides some methods that can be accessed by other objects to change its internal data. In Java, there are 4 modifiers default, public, private, and protected . Each modifier gives different access rights to other objects located in the same package or under different packages.
  2. Inheritance
    Inheritance provides objects with the ability to obtain fields and methods from the base class (parent class). Inheritance provides code reusability. You can also add new properties to existing classes without modifying the class.
  3. Polymorphism
    refers to the ability of multiple different underlying implementation classes to implement the same data type. An operation of a polymorphic type can be applied to other types of interfaces that implement the same data type.
  4. Abstract
    Separate ideas from concrete examples, and create subclasses for implementation according to different requirements and functions

What is the difference between object-oriented and process-oriented?

Process-oriented:

  1. Advantages: The performance is higher than that of object-oriented, which avoids the instantiation required for class calls, but the overhead is large and resources are consumed.
  2. Disadvantages: not easy to maintain, reuse and expand

Process-oriented:

  1. Advantages: easy to maintain, reuse and expand. Due to the four object-oriented features, a low-coupling system can be designed to make the system more flexible
  2. Disadvantages: performance is lower than process-oriented

The difference between overloading and rewriting?

Override override :

  1. Same method name, parameters, and return value
  2. The subclass cannot reduce the access rights of the parent class, and the subclass cannot throw more exceptions than the parent class method
  3. Methods that are final modified cannot be rewritten

Overload overload

  1. At least one of the parameter type, number, and order is different
  2. Cannot overload method names with different return values

What is a construction method in Java? What is constructor overloading

  1. Construction method
    When a new object is created, the construction method will be initialized by the Java compiler by default, coupled with no-argument construction method
  2. Constructor
    overloading is very similar to method overloading. You can create multiple constructors for a class, but the parameter list of each constructor must be unique.

What is the relationship between JDK, JRE, and JVM?

  1. JDK is a toolkit developed by java, which contains the compiling and running environments required by java programs. Simply put, JDK contains JRE contains JVM
  2. JRE is the running environment of java, including the java virtual machine (JVM) and rich system library. The system class library is the functional class encapsulated by java in advance, which is used immediately, which greatly improves the efficiency of development
  3. JVM is a java virtual machine, which provides a running environment for bytecode files

What is bytecode? The biggest benefit of using bytecode?

The concept of virtual machine is introduced in java, that is, an abstract machine is added between the machine and the compiler. This virtual machine provides a compiler on any platform with a common interface
. After the compiler needs to face the virtual machine, generate code that the virtual machine can understand, and then the interpreter converts the virtual machine code into the machine code of the specific system carried out. In Java, this code for the virtual machine to understand is called bytecode, which is not oriented to any specific processor, but only oriented to the virtual machine.
The interpreter for each platform is different, but the virtual machine implemented is the same. The Java program is compiled into bytecode by a compiler. The bytecode is interpreted by the virtual machine, and each executed bytecode is sent to the interpreter, and the interpreter translates it into the machine code of a specific operating system for execution, so Java is a cross-platform language.

Basic data types of JAVA

The data types supported by JAVA include basic data types and reference data types. The
basic data types are as follows:

  1. Integer type: byte, short, int, long
  2. Character type: char
  3. Floating point: float, double
  4. Boolean: boolean

The variable declared by the reference data type is actually a reference address in memory, and the entity is in the heap:

  1. Reference types include classes, interfaces, arrays, etc.
  2. String is a reference type, not a basic type

What is passing by value and passing by reference?

  1. Value transfer is for variables of basic data types, what is transferred is a copy of the variable, and changing this copy does not affect the original value
  2. Pass by reference, generally refers to the variable of the object type, passing an address in the sub-heap memory of the object, not the source object

Is it possible to access non-static variables in a static environment?

The static variable belongs to the class in Java, and it is the same in all instances. When the class is loaded by the java virtual machine, the static variables are initialized and copied (default value assignment).
If you do not try to use the instance to access non-static variables, the compiler reports an error because these variables have no real meaning at this time. Created on but not yet managed with the instance

// 此时在java虚拟机进行类加载准备阶段赋值时,默认赋值为0
private static int a = 1;

// 而被final修饰后,会在虚拟机中默认初始化一个ConstantValue进行赋值
private final static int a = 1;

What is the difference between String, StringBuffer and StringBuilder?

  1. String: A read-only string. The content of the string referenced by String cannot be modified because it is final modified. Every time the String type is changed, a new String object is generated, and then the pointer points to the new String object
  2. StringBuffer/StringBuilder: The identification string object can be directly modified. StringBuffer operates on its own object every time, instead of generating new objects and changing the references of the objects. Because StringBuffer is modified by synchronized, it is not thread-safe compared to StringBuilder, and its performance is only about 10-15% worse.

A common interview question

// 创建了几个对象
String s = new String("a");
// 首先,字符串a会在字符串常量池中寻找,若找不到则会创建一个对象
// 然后,因为有new关键字,所以会在内存中创建一个String对象,并将其返回给s
// 所以答案显然是1个或者2个

String is immutable, StringBuffer\StringBuilder is variable?

  1. The final keyword is used in the String class to modify the string array, so the String object is immutable
// String.java
private final char[] value;
  1. StringBuffer\StringBuilder inherits the AbstractStringBuilder class, and the string array is also used in AbstractStringBuilder, but it is not modified by final, so it is variable
// AbstractStringBuilder.java
private char[] value;

What is automatic unpacking?

Is the mutual conversion between basic data types and reference types

The difference between equals and ==?

  1. Value types (int, char, long, boolean, etc.) are all used == to judge equality
  2. Object reference:
    == Determine whether the object referred to by the reference is the same, that is, compare whether the address pointed to in the memory is the same. The
    equals method is used to determine whether the value of the object is equal

What is the difference between final, finally, and finalize?

  1. final: is a modifier keyword. If a class is declared as final, it means that it cannot derive new subclasses and cannot be inherited as a parent class. Therefore, a class cannot be declared as abstract and then final. The class declared as final will not be changed in use, and the initial value must be given at the time of declaration.
  2. finally: Provided in exception handling to perform clear operations. If an exception is thrown, it will match the corresponding catch clause for execution, and finally control will enter the finally block
  3. finalize: method name. Java allows the use of this method to do the necessary cleanup work before the garbage collector clears the object from memory. This method is called by the garbage collector when it confirms that the object is not referenced

What is the difference between abstract class and interface?

From a design point of view: abstract class is an abstraction of a class and a template design; an interface is an abstraction of behavior and a norm of behavior.
Differences:

  1. All methods in the interface are implicitly abstract, and abstract classes can contain both abstract and non-abstract methods
  2. A class can implement multiple interfaces, but can only inherit one abstract class
  3. The variables in the interface are final by default, and abstract classes can contain non-final variables
  4. The member functions in the interface are public by default, and the abstract class can be private, protected or public
  5. The interface is absolutely abstract and cannot be instantiated. Abstract classes cannot be instantiated

The order of instantiation of the class?

  1. Parent class static variable
  2. Parent class static code block
  3. Subclass static variable
  4. Subclass static code block
  5. Non-static variables of the parent class
  6. Parent class constructor
  7. Subclass non-static variables
  8. Subclass constructor

What is JAVA IO?

The specific operations of java io are divided into two ways: byte-oriented and character-oriented.
1. Byte stream:
InputStream (input stream)
OutputStream (output stream)
2. Character stream:
Reader (read)
Writer (write out)

What is JAVA serialization?

Serialization is actually a mechanism for processing object streams. The so-called object stream is to stream the content of objects:

  1. The streamed object can be read and written, and the streamed object can be transmitted to the network
  2. Serialization is to solve the problems caused when reading and writing the object stream

How to serialize?

1. Use Java's built-in serialization method to implement the Serializable interface,
and then use an output stream: FileOutputStream to construct an ObjectOutputStream object.
Use the writeObject method to write out and save the object that needs to be instantiated.
2. Deserialization: restore the input stream To restore

What if some fields do not need to be serialized in serialization?

Can be modified with the transient keyword

How to implement object cloning?

  1. Implement the Cloneable interface and rewrite the clone() method in the Object class to achieve shallow cloning
  2. Realize the Serializable interface, and realize cloning through the serialization and deserialization of the object. Deep clone

The purpose and realization of reflection?

  1. Construct an object of a class at runtime
  2. Determine the member variables and methods of a class
  3. Call a method of an object
  4. Generate dynamic proxy

There are many applications of reflection: for example

  1. Ioc in spring creates objects and sets dependency properties based on reflection
  2. The request of spring mvc calls the corresponding method

The difference between Class.forName and ClassLoader?

  1. Class.forName: In addition to loading the .class file of the class into the jvm, the class will also be interpreted and the static block in the class will be executed
  2. The classLoader can only do one thing, that is, load the .class file into the jvm, it will not execute the content in the static, and only execute the static block in the newInstance

How does JAVA create objects?

  1. Use the new keyword to create an object
  2. Use the newInstance method of the class class (reflection mechanism)
  3. Use the newInstance method of the Constructor class (reflection mechanism)
  4. Use the clone method
  5. Created using deserialization

Guess you like

Origin blog.csdn.net/qq_37629227/article/details/112620160