2021/1/11 Niuke hundred questions summary

Recently, I found that reading books is really boring, and the knowledge points in it are particularly easy to forget. Therefore, I started the special practice of brushing the cattle. At the beginning, the standard set is about one hour a day.
It takes about 15-20 minutes to make a set of 30-question papers, so I brush a hundred questions a day + a summary every day + a review of yesterday’s content every day. I am also considering whether to use the Ebbinghaus memory method. Let's look at the situation.

1. Internal classes can also use four access permissions

public:同一类、同一包、不同包子类、不同包非子类
protected:同一类、同一包、不同包子类
默认:同一类、同一包
private:同一类

2. About return in try

public class Test{
    
    	
    public int add(int a,int b){
    
    	
         try {
    
    	
             return a+b;		
         } 
        catch (Exception e) {
    
    	
            System.out.println("catch语句块");	
         }	
         finally{
    
    	
             System.out.println("finally语句块");	
         }	
         return 0;	
    } 
     public static void main(String argv[]){
    
     
         Test test =new Test(); 
         System.out.println("和是:"+test.add(9, 34)); 
     }
}

Execution result:
finally statement block
and is: 43 is
mainly because return does not return directly when the statement in the try is executed, but first puts the result of return into a temporary stack, and then executes the finally statement, and finally returns.

public abstract class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(beforeFinally());
    }
    
    public static int beforeFinally(){
    
    
        int a = 0;
        try{
    
    
            a = 1;
            return a;
        }finally{
    
    
            a = 2;
        }
    }
}
/**output:
1
*/

Just like this code, finally will only cover a, but a has been stored in the temporary stack before that, so it does not affect return.
Regarding the return problem in finally, I remember that the book "In-depth understanding of the Java Virtual Machine" seems to say that it will not cover the return in the try, but the "Java Interview Assault" on Github seems to say that the return in the finally will cover the try The return.

3. The array will be initialized

4. The member variables in the interface default to public static final, and the method defaults to public abstract

5. The Join method in Thread is to wait for the end of the thread

For example, if a.join() is called in the b thread, then the b thread will not execute until the a thread ends.

下列代码执行结果为() 
public static void main(String args[])throws InterruptedException{
    
    
	    	Thread t=new Thread(new Runnable() {
    
    
				public void run() {
    
    
					try {
    
    
						Thread.sleep(2000);
					} catch (InterruptedException e) {
    
    
						throw new RuntimeException(e);
					}
					System.out.print("2");
				}
			});
	    	t.start();
	    	
	    	t.join();
	    	System.out.print("1");
	    }
	  //  OutPut:21

6.valueOf method: return a String object

public class CharToString {
    
    
 public static void main(String[] args)
 {
    
    
  char myChar = 'g';
  String myStr = Character.toString(myChar);
  System.out.println("String is: "+myStr);
  myStr = String.valueOf(myChar);
  System.out.println("String is: "+myStr);
 }
}
//Output:
		String is g
		String is g

7. The construction method cannot be inherited (but there is also a saying that the child class inherits everything from the parent class, but the private is not accessible, just look at it dialectically)

8.<<Shift left, >> shift right with sign, >>> shift right without sign

9.wait(), notify() and notifyAll() are methods in the Object class

Condition only appeared in java 1.5. It is used to replace the traditional Object's wait() and notify() to achieve inter-thread cooperation. Compared with the use of Object's wait() and notify(), use Condition1's await() , Signal() This way to achieve inter-thread collaboration is safer and more efficient.

10.Java exception class

Insert picture description here
Non-RuntimeException is generally an external error, which must be caught by the try{}catch block.

RuntimeException, runtime exception, Java compiler will not check.

11. Collection interface level
Insert picture description here
12. JVM memory includes virtual machine stack, local method stack, program counter, method area and heap

JVM堆分为:新生代(一般是一个Eden区,两个Survivor区),老年代(old区)

13.Swing

Swing is a newly developed package to solve the problems of AWT. It is a Java-based cross-platform MVC framework. It uses single-threaded mode, but it runs slower than AWT. All its components are inherited from the javax.swing.JComponet class. .

14. foward and redirect problem

foward: The server obtains the content of the redirected page and sends it to the user, and the user address bar remains unchanged

redirect: The server sends the redirect address to the user, and the address bar becomes the new address after redirection

15. Multiple inheritance concept

Multiple inheritance in Java is achieved in three ways:

1. Implement multiple interfaces

2. Inherit a class, and then implement one or more interfaces

3. Inherit other classes through internal classes

16.servlet service

1. Both post and get methods will be processed in the service

2. The service judges the request type. If it is a post, it executes the doPost method, otherwise it executes the doGet method. DoPost and doGet are implemented in Httpservlet.

service方法是在servlet生命周期的服务期

servlet生命周期分为三个阶段:

1.初始化阶段,调用init()方法

2.响应客户请求阶段,调用service()方法

3.终止阶段,调用destroy()方法

servletContext存放共享数据,是一个全局变量,提供一系列方法供Servlet与Web容器交互

servletConfig封装servlet配置信息

17 The imported package can only be imported to the current layer, and the bread class in the package cannot be imported

18.

public static void main(String[] args) {
    
    
    String a = new String("myString");
    String b = "myString";
    String c = "my" + "String";
    String d = c;
    System.out.print(a == b);
    System.out.print(a == c);
    System.out.print(b == c);
    System.out.print(b == d);
}

A: a points to the heap memory, b points to the constant pool, so the addresses are not equal, false
B: java has a constant optimization mechanism , c also points to the constant pool, and b points to the same, then a and c addresses are not equal, false;
C : B and c address are equal, true
D: d is a copy of c, the address is the same, so b and d address are equal, true

19. Local variables are created when the statement is executed, not when the method is executed

20. Only new and reflection use construction methods

Guess you like

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