30-day check-in for interview questions-day13

1. What are hard links and soft links in Linux, and what is the difference between them?

Under the Linux system, there are two kinds of link files, one is a hard link (Hard Link), and the other is a soft link, also known as a symbolic link (Symbolic Link). Their differences are as follows:

Hard link:

  • Hard links are linked through inode nodes in the file system, so hard links and source files have the same inode node number.
  • Hard links can only be created between files in the same file system, not directories.
  • There can be multiple hard links pointing to the same inode node, or a file can have multiple path names, so a file can correspond to multiple file names.
  • Deleting the source file or any hard links will not affect the availability of other links, the inode node will only be released when all links have been deleted.

Soft link:

  • A soft link is a file that contains the pathname of another file, and it refers to another file by its name.
  • Soft links can link files in different file systems.
  • When performing a read or write operation on a soft link, the system will automatically convert the operation into an operation on the source file.
  • When deleting a soft link, only the linked file will be deleted, and the source file itself will not be deleted.
  • A soft link can point to a file that doesn't exist.

The difference between the two is as follows:

  1. In the file system, a hard link is a different file name pointing to the same inode, while a soft link is a file (shortcut) that contains the path to the target file.
  2. Hard links can only link to files in the same file system, and soft links can link to files on different file systems.
  3. Deleting a hard link will not affect other hard links or real files, while deleting a soft link will make the linked target file or directory an orphan file, unless the target file or directory also has other links.

2. Could you please introduce the JVM memory model, which areas are divided into? What is the role of the regions?

The JVM memory area is divided into five parts, namely the heap, the method to go, the virtual machine stack, the local method stack, and the program counter

image-20230304154805418

  • heap. The heap is the storage area for Java objects. Any Java object instances and arrays allocated with the new field are allocated on the heap. The Java heap can use -Xms -Xmx for memory control. It is worth mentioning that since JDK1.7 version , the runtime constant pool is moved from the method area to the heap.
  • method area. It is used to store data such as class information loaded by the virtual machine, constants, static variables, and code compiled by the just-in-time compiler. Literal and symbolic references. The method area was called the permanent generation in JDK1.7 and before, and was removed from the JDK1.8 permanent generation.
  • virtual machine stack. When each method is executed in the virtual machine stack, a stack frame is created to store local variable table, operand stack, dynamic link, method exit and other information. Each thread has an independent virtual machine stack, and the size of the stack can be set by the -Xss parameter. The virtual machine stack is divided into stack frames. When each method is executed, a stack frame is created to store information such as local variables and operand stacks.
  • Native method stack. Similar to the role played by the virtual machine stack, compared to the virtual machine stack serving the Java method, the local method stack serves the Native method used by the virtual machine. When each local method is executed, a stack frame is created to store local variables. Table, operand stack, dynamic link, method exit and other information.
  • program counter. The program counter is a small memory area that is used to record the address of the bytecode instruction executed by the current thread and indicate the next bytecode instruction that needs to be executed by the Java virtual machine. Each thread has an independent program counter, which is used to ensure that execution can resume after a thread switch.

The above five areas are the memory division of the Java virtual machine. The method area and the heap are shared by multiple threads in the JVM. For example, the static constants of the class are stored in the method area for sharing between class objects, virtual machine stacks, and local methods. The stack and pc registers are owned independently by each thread and will not be shared with other threads.
So when Java creates a class object instance through new, on the one hand, it will create a reference to the object in the virtual machine stack, on the other hand, it will create an instance of the class object on the heap, and then point the object reference to the instance of the object . Object references are stored in the stack frame corresponding to each method.

3. What annotations can be injected into Bean? What is the difference between @Autowired and @Resource?

In the Spring framework, commonly used annotations for injecting beans include:

  1. @Autowired: Automatic injection, automatic assembly according to type, if there are multiple beans of the same type, you need to specify a specific bean through @Qualifier.
  2. @Resource: The injection method that comes with Java is automatically assembled according to the name. The default is to match according to the property name. If you need to match according to the Bean name, you can use @Resource(name="beanName").
  3. @Inject: Similar to @Autowired, it is also automatically assembled according to the type, but the @Inject annotation is provided by JSR-330, while the @Autowired annotation is provided by the Spring framework.
  4. @Value: used to inject the attribute value in the configuration file, and the default value can be specified.
  5. @Component: Used to declare a Bean, which acts like a tag in XML.

All of the above annotations can be used to inject beans, and the difference between different annotations mainly lies in the different injection methods and implementation methods. @Autowired and @Resource are the most commonly used. Among them, @Autowired is more commonly used for autowiring by type, while @Resource is more suitable for cases where you need to specify the Bean name explicitly.

Baidu's answer:

  1. @Autowired: Spring officially recommends the way of injecting beans, which injects beans into the classes that need to be used through automatic assembly.
  2. @Resource: The way of injecting beans provided in the JSR-250 specification can be injected by specifying the name or type of the bean.
  3. @Inject: The way of injecting beans provided in the JSR-330 specification is similar to @Autowired, but more flexible.
  4. @Qualifier: Used with @Autowired to inject by specifying the name of the Bean.
  5. @Value: Inject the field or method parameter in the Bean through the attribute value in the configuration file.
  6. @Component: Used to declare a Bean, which acts like a tag in XML.

The difference between @Autowired and @Resource

  1. The @Autowired annotation is provided by Spring, while the @Resource annotation is provided by J2EE itself
  2. The @Autowird annotation is injected by byType by default, while the @Resource annotation is injected byName by default
  3. The object injected by the @Autowired annotation needs to exist in the IOC container. Otherwise, the attribute required=false needs to be added to indicate that the current bean to be injected is ignored.

Exist in the IOC container, otherwise, you need to add the attribute required=false, which means ignoring the current bean to be injected, if there is a direct injection, if not, skip it, and no error will be reported

Guess you like

Origin blog.csdn.net/qq_56098191/article/details/130393507