java interviewer want to hear the answer-java basic interview questions


I am not an interviewer, but also a chicken who has to go through one interviewer after another. Sometimes you are just answering the interviewer ’s question, do you have the following three questions about the soul?

我的回答有错么?
我的回答够了么?
是面试官想听的结果么?

If you can see this article, it may not be enough. So I started from the interviewer's blog and combined my knowledge to re-answer an answer that the interviewer gave you. I will never finish writing once. I will continue to update my blog. I hope to study with you. I hope you can correct me if you see errors

java basic problem

1. The difference between overloading and rewriting?

Overloading and rewriting are actually ways to achieve polymorphism, the difference is that the heavy carrier is now polymorphic at compile time, and rewriting is reflected in polymorphism at runtime.
Overloading occurs in a class. Methods of the same name differ according to parameters (number of parameters, type of parameters, ordering of parameters), to achieve the purpose of overloading, note that overloading does not require return parameters, and cannot be distinguished by return parameters Overloaded.
The rewriting occurs between the subclass and the parent class. The subclass rewrites the same parameter list of the parent class and has a compatible return type method. At the same time, the access rights of the subclass must be> = the access rights of the parent class, such as those four Access modifier.

2. What is the difference between an interface and an abstract class?

Neither the interface nor the abstract class can be instantiated. If you want to instantiate, the interface needs to have all the abstract methods of the implementation class to implement the interface. The abstract class needs subclasses to inherit and implement all abstract methods of the abstract class. If not all are implemented, it is still an abstract class.
The way of interfaces and abstract classes, one implementation, one inheritance, single inheritance, multiple implementation
interfaces can only define abstract methods and static constants, and abstract methods can exist in abstract classes, ordinary methods, and ordinary variables can also exist .
It can also be seen from the above that the degree of abstraction of the interface is relatively high.
Usually interfaces are used to abstract functions, and abstract classes are used to abstract categories. For example, the BeanFactory abstract factory pattern in spring produces different types of Bean objects.

3. What is aspect-oriented?

Interviewer: Explain what is the aspect-oriented, plus points, explain the principle of the agent, plus points
I: For the business logic of the system and system services that are used repeatedly, the business logic serves as the aspect, adding system services before and after the aspect. For example, Spring AOP, the method is the smallest granularity, you can add @Before and @After before and after the method to add logic used repeatedly, such as logs, transactions. In fact, aop is the dynamic proxy used.

There are jdk dynamic proxy Implement InvocationHandler and cglib dynamic proxy implements MethodInterceptor in java. The proxy class implements these two interfaces, and there are return proxy methods and invoke methods inside.

jdk dynamic proxy principle (the proxy class must have an implementation interface): get all the interfaces of the proxy object by reflection, regenerate a class, and implement all the interfaces of the proxy class, and then dynamically generate java code and compile Form a class file and reload it into the jvm. The above is the bytecode reorganization.
cglib dynamic proxy: adopt the way of inheriting the proxy class.

Both are achieved by dynamically generating bytecode at runtime.

4. What is serialization? When is serialization required?

Serialization: The process of converting java objects into byte streams. Deserialization: The process of converting a byte stream into a java object.
Scenario: When java objects need to be transmitted on the network or need to be persisted to a file and written to the database, the java objects need to be serialized.
Implementation: The class can implement the Serializable interface.
note:

  1. A certain class can be serialized, then its subclasses can also be serialized
  2. Member variables declared as static and transient cannot be serialized. static member variables are attributes that describe the class level, transient means temporary data
  3. Deserialize the sequence of reading serialized objects to be consistent
5. What are the ways to create objects in java?
  1. Using the new keyword, you can call the constructor you want to call (including parameterless and parameterized constructors).
  2. Using the newInstance () method of the Class class, the parameterless construction method will be called.
Employee emp2 = (Employee) Class.forName("com.xiaodeng.pojo.Employee").newInstance();
  1. Using the newInstance () method of the Constructor class, you can call parametric constructors and private constructors.
Constructor constructor = Employee.class.getConstructor(); 
Employee emp3 = constructor.newInstance();
  1. Use clone () method, this method does not call any construction method, pay attention to deep cloning and shallow cloning!
//使用clone对象时,该对象需要实现Cloneable接口,并实现clone方法,如果直接返回super.clone方法,
//复制出来的对象虽不一样,但对象中的字段用的还是之前的对象的属性字段,叫浅克隆
Employee emp4 = (Employee) emp2.clone();
  1. Using serialization and deserialization to create objects can solve the problem of shallow cloning. The JVM does not use a constructor to create an independent object.
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj")); 
Employee emp5 = (Employee) in.readObject();
6. What is reflection? There are three ways to create class objects by reflection. What are the methods of reflection?

Reflection: In the running state of reflection, for any class, you can know all the properties and methods of this class, and for any object, you can call any of its properties and methods.

//创建class对象的三种方式:
Class<?> clazz1 = Class.forName("test.Counter");//全包名
		Class<?> clazz2 = Counter.class;//类名.class
		Counter counter = new Counter();
		Class<?> clazz3 = counter.getClass();//对象.getclass()
//反射创建对象
		clazz1.newInstance();//调用无参构造方法
		clazz1.getConstructor().newInstance(1);//可以调用有参和私有的构造方法
//获取对象的属性
		clazz.getFields();//获得某个类的所有的公共(public)的字段,包括父类中的字段。
		clazz.getDeclaredFields();//获得某个类的所有声明的字段,但是不包括父类的声明字段.
//通过class对象获得一个方法对象
		clazz.getMethod(“方法名”,参数类型);//只能获取公共的)
		clazz.getDeclareMethod(“方法名”);//获取任意修饰的方法,但是此时还不能执行私有)
		M.setAccessible(true);//让私有的方法可以执行
		Method.invoke(obj实例对象,obj可变参数);//执行对象的某个方法,可以返回参数
7. Talk about your understanding of the singleton pattern?

In a system, the singleton pattern ensures that there is only one instance of a class. For example, Spring, when the container starts, it completes the positioning and loading registration of BeanDefine, and then getBean returns the singleton mode object Bean by default (of course, this Bean is not a direct new object, but a proxy object returned).

At the same time, since there is only one instance of a class, the original intention of using singleton mode is resource sharing, and an object is used together in a container, but resource sharing must pay attention to thread safety!
For example: I know there are three kinds of singleton mode implementations:
1. Lazy singletons: the container does not create objects by default, and only create objects when they are used, such as the classic double check singleton mode, because the new object The problem of ordering is that the thread is not safe.

public class Singleton {
  static Singleton instance;
  static Singleton getInstance(){
    if (instance == null) {
      synchronized(Singleton.class) {
        if (instance == null)
          instance = new Singleton();//这里存在线程安全问题
        }
    }
    return instance;
  }
}

Using anonymous inner classes to achieve lazy loading

在调用getinstance方式时,再会加载内部类。
public class LazyThree {
	public static final LazyThree getinstance() {		
		return LazyHolder.lazy;// 在返回内部类前,一定会先加载内部类
	}
	private static class LazyHolder {// 默认这个内部类不加载
		private static final LazyThree lazy = new LazyThree();
	}
}
  1. Hungry, that is, whether I use an instance object or not, I create it first, and then use it to return to the object I created before.
  2. The registered singleton mode is used by Sping's ioc (control inversion). In fact, ioc is a container for storing BeanDefined, and internally it is a thread-safe ConcurrentHashMap. If you use an object, find it from the container, create an object and return it, and put it in the container, and directly take it out of the container the next time you use it.
8. What is the difference between actual and formal parameters?

Insert picture description here
The actual parameters are defined outside the method. When the method is called, the actual parameters are passed in and the method is called. At this time, the memory for the formal parameter variables is allocated. At the end of the method call, the allocated memory is released. The formal parameters can only be in the method function Valid, the parameter cannot be used again after the method ends.

Come here today, if you have any questions, you can leave a message in the comment area, thank you.

More: Deng Xin

Published 34 original articles · Likes0 · Visits 1089

Guess you like

Origin blog.csdn.net/qq_42634696/article/details/104721870