2021/1/12 100 questions summary

I do not produce code, I am just a code porter

1. The difference between different creation methods of Integer

There are three ways to create Integer, namely

Integer a=new Integer(100);
Integer b=100;
Integer c=Integer.valueOf(100)

Compare below:

Integer a1=new Integer(100);
Integer b1=100;
Integer c1=Integer.valueOf(100)
a==a1 //Output:false 因为new创建的是引用对象,两个引用对象比较堆中地址,地址不同;
b==b1//Output:true,但是将b与b1改成400后结果为false
c==c1//结果与b相同
  • This is due to the source code of valueOf
public static Integer valueOf(int i) {
    
        
       if(i >= -128 && i <= IntegerCache.high)    
           return IntegerCache.cache[i + 128];    
       else    
           return new Integer(i);    
}   
  • When the valueOf or the number of auto-boxing is between -128 and 127, the memory will be viewed directly, so it will not use new to create a new object, and if it exceeds the range, it will be new, so the result is false

For example:

Integer i01=59;
int i02=59;
Integer i03=Integer.valueOf(59);
Integer i04=new Integer(59);
// i01==i02==i03,i04与上述三个不相等

2. Several characteristics of the construction method

1. The construction method can be overloaded, a construction method can call another construction method through the this keyword, this statement must be located in the first line of the construction method; (but cannot be overridden)

2. When no construction method is defined in a class, Java will automatically provide a default construction method;

3. The subclass calls a construction method of the parent class through the super keyword;

4. When a certain construction method of the subclass does not call the construction method of the parent class through the super keyword, when the subclass object is created through this construction method, the default construction method of the parent class will be automatically called first

5. The construction method cannot be modified by static, final, synchronized, abstract, native, but can be modified by public, protected, default, and private;

6. The constructor is not a member method of the class;

7. The construction method cannot be inherited.

3. Several classes and interfaces are defined in a Java source program file, then after compiling the file, several bytecode files with .class suffix will be generated

Insert picture description here

4.PreparedStatement knowledge points

PreparedStatement is precompiled, and there are several advantages to using PreparedStatement

a. When executing a piece of SQL with variable parameters, PreparedStatement is more efficient than Statement, because DBMS pre-compiling a piece of SQL will of course be more efficient than compiling a piece of SQL multiple times.

b. Good security, effectively preventing Sql injection and other problems.

c. For statements that are executed repeatedly, using PreparedStament will be more efficient, and in this case it is also more suitable to use batch;

d. The readability and maintainability of the code.

  • The CallableStatement interface extends PreparedStatement to call stored procedures. It provides support for output and input/output parameters. The CallableStatement
    interface also has support for the input parameters provided by the PreparedStatement interface.

Inheritance relationship:
Insert picture description here

5.Maven and Ant

  • Ant and Maven are both Java-based build tools. In theory, it is somewhat similar to make in (Unix) C, but without the flaws of make. Ant is a software construction tool, and Maven is positioned as a software project management and understanding tool.
  • The Ant build file is named build.xml by default, and the Maven build file is pom.xml by default.
  • Ant features:
  • 1. There is no agreed directory structure
    . 2. It must be clear what ant does, when to do it, and then compiled and packaged
    . 3. There is no life cycle, and the goal and the task sequence to be realized must be defined
    . 4. There is no integrated dependency management. Maven features:
    1 . Have a convention, know where your code is and where to put it
    2. Have a life cycle, for example, execute mvn install to automatically execute the build process such as compilation, testing, packaging, etc.
    3. Only need to define a pom.xml, and then Put the source code in the default directory, Maven will help you handle other things
    4. Have dependency management, warehouse management

6. Regular expression characters

Introduction to characters in regular expressions

7. Modifiers in Java

Insert picture description here

8. The benefits of using generics

1. Type safety.

  • The main goal of generics is to improve the type safety of Java programs. By knowing the type restrictions of variables defined using generics, the compiler can verify type assumptions to a much higher degree. Without generics, these assumptions only exist in the programmer's mind (or, if you are lucky, in the code comments).

2. Eliminate forced type conversion.

  • A side benefit of generics is that they eliminate many coercive types in the source code. This makes the code more readable and reduces the chance of errors.

3. Potential performance gains.

  • Generics bring the possibility of greater optimization. In the initial implementation of generics, the compiler will force type conversion (if there is no generic type, the programmer will specify these type conversions) into the generated bytecode. But the fact that more type information is available to the compiler makes it possible to optimize future versions of the JVM. Due to the implementation of generics, support for generics (almost) does not require JVM or class file changes. All work is done in the compiler, and the compiler generates code similar to the code written without generics (and coercive type conversion), but it is more type-safe.

9. Class loading order

public class Base
{
    
    
    private String baseName = "base";
    public Base()
    {
    
    
        callName();
    }

    public void callName()
    {
    
    
        System. out. println(baseName);
    }

    static class Sub extends Base
    {
    
    
        private String baseName = "sub";
        public void callName()
        {
    
    
            System. out. println (baseName) ;
        }
    }
    public static void main(String[] args)
    {
    
    
        Base b = new Sub();
    }
}

1. Loading order of classes:

(1) The parent class static code block (including static initialization block, static properties, but not static methods)

(2) Subclass static code blocks (including static initialization blocks, static properties, but not static methods)

(3) Non-static code blocks of the parent class (including non-static initialization blocks and non-static attributes)

(4) Parent class constructor

(5) Subclass non-static code block (including non-static initialization block, non-static attribute)

(6) Subclass constructor

Among them: the static blocks in the class are executed in the order of declaration, and (1) and (2) are executed when the new class instance does not need to be called (meaning it is executed when the class is loaded into the method area)

2. Secondly, you need to understand the problem of subclasses overriding the methods of the parent class, that is, the problem of polymorphism by method rewriting.

Base b = new Sub(); It is a manifestation of polymorphism. The declaration is Base and the implementation is the Sub class. It is understood that b is shown as a Base class characteristic at compile time, and as a Sub class characteristic at runtime.

When the subclass covers the method of the parent class, it means that the method of the parent class has been overridden. The method called by the parent class in the question is the method implemented by the subclass, and the baseName called in the method implemented by the subclass is in the subclass. Private properties.

It can be seen from 1. that only step 4 is executed at this time, the non-static code block of the subclass and the initialization step have not yet arrived, and the baseName in the subclass has not been initialized. So baseName is empty at this time. So it is null.

10. Garbage collection mechanism

Insert picture description here

1, Cenozoic:

(1) All objects are created in the Eden area of ​​the new generation. When the Eden area is full, the new generation Minor GC is triggered, and the surviving objects in the Eden area and the non-free Survivor area are copied to another free Survivor area.

(2) To ensure that a Survivor area is empty, the Cenozoic Minor GC is to copy surviving objects between the two Survivor areas until the Survivor area is full.

2. Old age:

When the Survivor area is full, the objects are copied to the old generation through the Minor GC. If the old age is full, Full GC will be triggered to perform garbage collection for the entire heap (including young, old, and persistent).

3, Endurance:

If the persistent generation is full, Full GC will be triggered.

11.synchronized mechanism

public class Test {
    
    
    private synchronized void a() {
    
    
    }
    private void b() {
    
    
        synchronized (this) {
    
    
        }
    }
    private synchronized static void c() {
    
    
    }
    private void d() {
    
    
        synchronized (Test.class) {
    
    
        }
    }
}

//修饰非静态方法 锁的是this 对象

//修饰静态方法 锁的是class对象

12. The return problem in try-catch-finally

Situation 1:

  • try{} catch(){}finally{} return;
  • Obviously the program is executed in order.

Situation 2:

  • try{ return; }catch(){} finally{} return;
  • Execute the return statement in the try block (including the expression operation in the return statement), but do not return;
  • Execute all the code in the finally statement and finally execute the return in the try.
  • The statement return after the finally block is not executed because the program has already returned in the try.

Situation 3:

  • try{ } catch(){return;} finally{} return;
  • The program executes try first, and executes the catch block if an exception is encountered,
  • There is an exception:
  • Execute the return statement in catch, but do not return
  • Execute all the codes in the finally statement, and finally execute the return in the catch block.
  • The return statement after the finally block is no longer executed.
  • No abnormality:
  • After executing try, finally return again...

Situation 4:

  • try{ return; }catch(){} finally{return;}

  • Execute the return statement of the try block (including the expression operation in the return statement), but does not return;

  • Then execute the finally block,

  • Execute the finally block, with return, return from here.

  • At this time, the return value of the finally block is the value after the code is executed
    5:

  • try{} catch(){return;}finally{return;}

  • The program executes the return statement in the catch block (including the expression operation in the return statement), but does not return;

  • Then execute the finally block,

  • Execute the finally block, with return, return from here.
    Situation 6:

  • try{ return;}catch(){return;} finally{return;}

  • The program executes the return statement in the try block (including the expression operation in the return statement), but does not return

  • There is an exception:

  • Execute the return statement in the catch block (including the expression operation in the return statement), but do not return;

  • Execute finally block

  • Execute the finally block, with return, return from here.

  • No abnormality:

  • Then execute the finally block to execute the finally block, with return, return from here.

13.String and == problem

public class Demo {
    
    
    public static void main(String args[]) {
    
    
        String str1 = new String("hello");
        String str2 = new String("hello");
        String str3 = "hello";
        String str4 = "hello";
        String str5 = "he"+"llo";
        String str6 = "he";
        String str7 = "llo";
        System.out.println(str1==str2);
        System.out.println(str1==str3);
        System.out.println(str3==str4);
        System.out.println(str3=="hello");
        System.out.println(str4==(str6+str7));
    }
}

The new String object is stored in the heap.

String str3="hello" is stored in the string constant pool.

String str5="he"+"llo" Because String is unchangeable, this String object is new.

13.Stream、File与IO

Insert picture description here

14. byte b = (byte)129

1. Forced type conversion

Cast int type to byte type

00000000 00000000 00000000 10000001 Only the last 8 bits are reserved 100000001

2. Source code, complement, complement

Source code: 10000001

Inverse code: 11111110

Complement: 11111111

Result: -127

15. Method of loading the driver

  • Load driving method

  • 1.Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);

    2.DriverManager.registerDriver(new com.mysql.jdbc.Driver());

    3.System.setProperty(“jdbc.drivers”, “com.mysql.jdbc.Driver”);

16.Collection interface and Map interface

Insert picture description here

16. Child process and parent process

  • What the child process gets is that except the code segment is shared with the parent process, everything else is a copy of the parent process. All the resources of the child process inherit the parent process and get a copy of the parent process resources. The child process can get the parent process. All the heap and stack data, but the two do not share the address space. The two are separate processes. After inheriting the two, there is no connection. The child process runs separately; the threads of the process share the resources obtained by the process, but the threads have a small part of their own resources, which is the stack space. Save its running state and local automatic variables.

17. Thread lock mechanism

Reference: Java thread] Lock mechanism: synchronized, Lock, Condition

Guess you like

Origin blog.csdn.net/qq_52212721/article/details/112521986