1. Java basic interview questions

1. What are JDK, JRE, and JVM? 2. What are
the basic data in Java
? 3. What is the principle of automatic boxing and unboxing? 4.
The difference between object-oriented and process-oriented 6. Talk about the understanding of deep copy, shallow copy, and reference copy 7. Why must rewrite hashCode() method when rewriting equals() 8. Differences and applicable scenarios of String, StringBuffer, StringBuilder 9. String s1 = new String ("abc"); How many string objects are created in this sentence? 10. Will the code in finally be executed?





@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

1. What are JDK, JRE, and JVM?
JDK: Java software development kit, which includes JRE and some Java development tools.
JRE: Java runtime environment: includes the core class libraries required by JVM and Java programs.
JVM: Java virtual machine, which translates the .class file into the machine code of the corresponding platform and hands it to the operating system for execution

2. What are the basic data in Java
4 types of integers: byte, short, int, long
2 types of floating point: float, double
1 type of character: char
1 type of Boolean: boolean.

3. What is the principle of automatic boxing and unboxing
Boxing: wrap the basic types with their corresponding reference types; unboxing: convert the packaging type into a basic data type;
boxing is to call the valueOf() of the packaging class method, unboxing means calling the xxxValue() method.

4. The difference between object-oriented and process-oriented
Process-oriented: The process is divided into methods, and the problem is solved by executing the method
Object-oriented: First, the object is abstracted, and the method is called by the object to solve the problem.
Object-oriented development programs are generally easier to maintain, reuse, and expand.

5. Object-oriented features and characteristics
Encapsulation: hide the specific implementation details, and provide the function as a whole to the external use of the class

Inheritance: The subclass has all the properties and methods of the parent class but private properties and methods cannot be accessed. The subclass can have its own properties and methods and can override the methods of the parent class.
Polymorphism: The reference of the parent class points to the instance of the subclass, and the subclass rewrites the method of the parent class, and what is actually executed is the method covered by the subclass. Polymorphism cannot call a method that exists in the subclass but does not exist in the parent class;

5.1: Does reflection destroy encapsulation
Although reflection can obtain private methods and use methods, it can only be said that it is powerful and can guarantee that no errors will occur when calling private methods, but after calling a method without reflection, the method is not private that's it. It is still private and still not visible to subclasses.

6. Talk about the understanding of deep copy, shallow copy, and reference copy
Shallow copy: Create a new object on the heap, but the internal properties of the original object have reference types, and only the address of the object will be copied. Deep copy: Deep
copy Will completely copy the entire object, including the internal objects contained in this object.
Reference copy: Two different references point to the same object.

浅拷贝
实现Cloneable接口,重写clone方法,直接调用super.clone(),返回一个在堆上的新地址
public class Demo implements Cloneable{
    
    
	public Student student;
    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        return super.clone();//只拷贝了当前类的对象,没有拷贝类中的对象student.原类和新类公用一个student对象
    }
}
class Student {
    
    
    public String name;
    public int age;
}
深拷贝
public class Demo implements Cloneable{
    
    
    Student student;
    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        Demo clone = (Demo) super.clone();
        clone.setStudent((Student) clone.getStudent().clone());
        return clone;
    }
    public Student getStudent() {
    
    
        return student;
    }
    public void setStudent(Student student) {
    
    
        this.student = student;
    }
}

class Student implements Cloneable{
    
    
    public String name;
    public int age;

    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        return super.clone();
    }
}
引用拷贝
Student s1 = new Student;
Student s2 = s1;

7. Why must rewrite hashCode() method when rewriting equals()
? 1. The function of hashCode() is to obtain the hash code and improve the efficiency of comparison and collection (HashSet, HashMap).
If the hashCode values ​​​​of the two objects are not the same equal, the two objects must not be equal.
If the hashCode values ​​of two objects are equal, then the two objects are not necessarily equal (hash collision).
Two objects are considered equal if their hashCode values ​​are equal and the equals() method also returns true.
2. If the hashCode() method is not rewritten when rewriting equals(), problems may occur when using HashMap.

8. Differences and applicable scenarios of String, StringBuffer, StringBuilder
1. Internal array modification keywords are different: String is immutable, internally maintains a final modified char array private final char value[];StringBuilder and StringBuffer both inherit from the AbstractStringBuilder class, in this class Also use char array to save strings, but not use final and private decoration
2. Thread safety: Objects in String are constants, thread safe, StringBuilder is thread unsafe, StringBuffer is thread safe.
3. Efficiency: StringBuilder is more efficient than StringBuffer in a single thread.
Applicable scenarios:
String: operate a small amount of data
StringBuilder: operate a large amount of data in a single-threaded operation string buffer
StringBuffer: operate a large amount of data in a multi-threaded operation string buffer

9.String s1 = new String("abc"); How many string objects are created in this sentence?
Create the corresponding string object in the heap and save the reference of the string object to the string constant pool.

对于 String str3 = "aa" + "bb"; 编译器会给你优化成 String str3 = "aabb"; 
引用的值在程序编译期是无法确定的,编译器无法对其进行优化。
        String s1 = "aa";
        String s2 = "bb";
        String s3 = "aabb";
        String s4 = "aa" + "bb";
        String s5 = s1 + s2;
        System.out.println(s3 == s4);//false
        System.out.println(s3 == s5);//true

10. Will the code in finally be executed?
Not necessarily:
the thread where the program is located dies.
Turn off the CPU.

Guess you like

Origin blog.csdn.net/m0_56182317/article/details/130111692