100 classic Java interviews, easy to get BAT-high-frequency interview questions

1. What is the Java Virtual Machine? Why is Java called a "platform-independent programming language"?
The Java virtual machine is a virtual machine process that can execute Java bytecode. Java source files are compiled into bytecode files that can be executed by the Java virtual machine.
Java is designed to allow applications to run on any platform without requiring programmers to rewrite or recompile each platform individually. The Java Virtual Machine makes this possible because it knows the instruction length and characteristics of the underlying hardware platform.
2. What is the difference between JDK and JRE?
The Java Runtime Environment (JRE) is a Java virtual machine that will execute Java programs. It also contains the browser plug-ins needed to execute the applet.
The Java Development Kit (JDK) is a complete Java software development kit, including JRE, compiler and other tools (such as JavaDoc, Java debugger), allowing developers to develop, compile, and execute Java applications.
3. What does the "static" keyword mean? Is it possible to override a private or
static method in Java ?
The "static" keyword indicates that a member variable or member method can be accessed without an instance variable of the class to which it belongs.
Static methods in Java cannot be overridden, because method overriding is based on dynamic binding at runtime, while static methods are statically bound at compile time. The static method is not related to any instance of the class, so it is conceptually not applicable. Private also does not support coverage, because private members are invisible to the outside world, so there is no coverage problem.
4. Is it possible to access non-static variables in a static environment?
A static variable belongs to a class in Java, and its value is the same in all instances. When the class is loaded by the Java virtual machine, the static variables are initialized. If your code tries to access non-static variables without an instance, the compiler will report an error because these variables have not been created yet and have not been associated with any instances.
5. What are the data types supported by Java? What is automatic unpacking?
The 8 basic data types supported by the Java language are:
integer type: byte short int long
floating point type: float double
Boolean type: boolean
character type : char
auto-boxing is one of the basic data types and corresponding object packaging types of the Java compiler An indirect transformation. For example: convert int
to Integer, double to double, and so on. The opposite is automatic unboxing.

6. What do method override and method overloading in Java mean?
Rules for rewriting methods: (Method rewriting is also called method coverage)
1. The parameter list must be exactly the same as the method being rewritten, otherwise it cannot be called rewriting but overloading.
2. The returned type must always be the same as the return type of the overridden method, otherwise it cannot be called an overwrite but an overload.
3. The restriction of the access modifier must be greater than the access modifier of the overridden method (public>protected>default>private)
4. The overridden method must not throw a new check exception or be broader than the overridden method declaration Checked abnormality. For example:
a method of the parent class declares a checked exception IOException. If this method is overridden, Exception cannot be thrown, only subclass exceptions of IOException can be thrown, and non-checked exceptions can be thrown.
The overloading rules:
1. Must have different parameter lists;
2. There can be return types that are not scolded, as long as the parameter lists are different;
3. There can be different access modifiers;
4. Different access modifiers can be thrown Exception;
the difference between rewriting and reloading is:
rewriting polymorphism works, and calling the overloaded method can greatly reduce the amount of code input. The same method name can have different parameters as long as different parameters are passed to it. The function or return value.
Good use of rewriting and overloading can design a class with a clear and concise structure. It can be said that rewriting and overloading play an extraordinary role in the process of writing code.
7. What is a constructor in Java? What is constructor overloading? What is a copy constructor?
When the new object is created, the constructor is called. Every class has a constructor. The programmer did not mention to the class
In the case of a constructor, the Java compiler will create a default constructor for this class.
Constructor overloading and method overloading in Java are very similar. You can create multiple constructors for a class. Each constructor must have its own unique parameter list. Java does not support copy constructors like in C++. This difference is because if you don't write the constructor yourself, Java will not create a default copy constructor.
8. Does Java support multiple inheritance? Do you support multiple implementations?
No, Java does not support multiple inheritance. Each class can only inherit one class, but it can implement multiple interfaces.
9. Can an interface inherit an interface? If it can be inherited, please cite a case?
Yes, List inherits Collection
10. What is the difference between interface and abstract class?
What is the difference between
an interface and an abstract class? On what basis did you choose to use an interface and an abstract class?
The concept of interface and abstract class is different. An interface is an abstraction of actions, and an abstract class is an abstraction of roots.
The abstract class represents what this object is. The interface expresses what this object can do. For example, Scottish Fold Cat, Ragdoll Cat, these two classes (if they are class...), their abstract class is cat. Explain that they are all cats.
Cats can eat, rats can also eat. You can define "eating" as an interface, and then let these classes implement it.
Therefore, in high-level languages, a class can only inherit one class (abstract class) ( Just as cats cannot be living and non-biological at the same time), they can implement multiple interfaces (eating interface, walking interface).
The first point. An interface is a variant of an abstract class, and all methods in the interface are abstract. An abstract class is a class that declares the existence of a method without realizing it.
Second point. Interface can be multiple inheritance, abstract class can not
The third point. Interface definition methods cannot be implemented, while abstract classes can implement some methods.
fourth point. The basic data type in the interface is static but the abstract image is not.
Fifth. The variables declared in the Java interface are final by default. Abstract classes can contain non-final variables.
Sixth point. The member functions in the Java interface are public by default. The member functions of abstract classes can be private, protected or public.
When you pay attention to the essence of a thing, use abstract classes; when you pay attention to an operation, use interfaces.
The function of abstract classes far exceeds that of interfaces, but the cost of defining abstract classes is high. Because high-level languages ​​(in terms of actual design also) each class can only inherit one class. In this class, you must inherit or write out all the commonalities of all its subclasses. Although the interface will weaken a lot in function, it is only a description of an action. And you can implement multiple interfaces in a class at the same time. It will reduce the difficulty in the design stage.
The interface is absolutely abstract and cannot be instantiated. Abstract classes cannot be instantiated.
11. What is value passing and reference passing?
The object is passed by value, which means that a copy of the object is passed. Therefore, even if the object copy is changed, the value of the source object will not be affected.
Objects are passed by reference, which means that what is passed is not the actual object, but the reference of the object. Therefore, external changes made to the referenced object will be reflected on all objects.