Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit punch card activity, click to seeEvent details.

👨🎓 author: Java Academic Conference

🏦 Storage: Github, Gitee

✏️ blogs: CSDN, Nuggets, InfoQ, Cloud + Community

💌 public account: Java Academic Party

🚫 special statement: the original is not easy, shall not be reproduced or copied without authorization, if you need to reproduce can contact xiaobian authorization.

🙏 Copyright notice: part of the text or pictures in the article come from the Internet and Baidu Encyclopedia, if there is infringement, please contact xiaobian as soon as possible. Wechat search public Java academic party contact xiaobian.

☠️ Daily toxic chicken Soup: this society is unfair, do not complain, because it is useless! People always make progress in introspection!

👋 Hello! I am your old friend Java academic Party, it is the best time of the year to find a job, have you got the offer? Based on the requirements of most of the fans, let the small compilation sort out some interview questions, as long as the fans have demand, it must meet, from today I will continue to update the interview questions, which covers: Java foundation, multi-threading, IO, high concurrency, collection framework, database, framework and distributed technology. Updated continuously

Java basic interview questions

1. Features of Java language

  • Object orientation: This is also the most important feature of Java, which enables programs to be less coupled and more cohesive.
  • Reliable security: The Java ecosystem includes tools for analyzing and reporting security issues.
  • Platform independent: Java can be used across platforms.
  • Support for multithreading: Java can use multithreading + coroutine to achieve more concurrent operations.
  • Easy to learn: Java has a rich class library, which can be encapsulated by static methods to reduce the cost of learning APIS and improve work efficiency.

2. The difference between object-oriented and procedural

  • From the concept: process oriented: the literal meaning is to face the process, what to do first, what to do, what to do finally, and then use the function to achieve these steps step by step, in the use of time one by one call can. Object oriented: The literal meaning is object oriented, is to constitute the problem of the transaction is broken down into various objects, but the purpose of building objects is not to complete a step, but to describe something in the process of solving the whole problem occurred behavior.

  • In terms of performance: process-oriented performance is higher, so microcontroller, embedded development and so on generally adopt process-oriented development. In terms of performance, object-oriented is lower than procedural.

  • From the point of availability: object-oriented encapsulation, inheritance, polymorphic characteristics, so easy to maintain, easy to reuse, easy to expand, can design a low coupling system.

3. The size of the eight basic data types, and their encapsulation classes

  • Int is the basic data type, and Integer is the wrapper class of int, which is the reference type. Int defaults to 0, and Integer defaults to null, so Integer can tell the difference between 0 and null. Once Java sees NULL, it knows that the reference does not yet point to an object, and must specify an object for any reference before it can be used, otherwise an error will be reported.

  • A basic data type is automatically allocated space when it is declared, while a reference type is only allocated reference space when it is declared. The value can be assigned only after the data space is created by instantiation. An array object is also a reference object. Assigning an array to another array simply copies a reference, so changes made in one array are visible in the other.

  • The Boolean data type is defined, but only very limited support is provided for it. There are no special bytecode instructions for Boolean values in the Java Virtual machine. Boolean values operated on by Java language expressions are replaced by Java VIRTUAL machine int data types after compilation. Boolean arrays are encoded as Java virtual machine byte arrays. Each element Boolean element has 8 bits. Thus we can conclude that the Boolean type takes up 4 bytes alone and 1 byte in the array. The reason for using int is that, for today’s 32-bit processors (cpus), data is processed 32 bits at a time (not on 32/64-bit systems, but on the CPU hardware level), which is highly accessible.

4. Talk about naming rules for identifiers

Identifier meaning: in the program, we define the content, such as class name, method name and variable name, etc., are identifiers.

Naming rules :(mandatory) identifiers can contain letters, digits from 0 to 9, and cannot start with a number. Identifiers are not keywords.

Naming conventions: (Optional) Class name conventions: capitalize the first character and the first letter of each subsequent word (big hump). Variable name specification: the first letter is lowercase, followed by the first letter of each word is capitalized (small hump). Method name specification: same variable name.

5. Be familiar with the instanceof keyword

Instanceof is strictly a binocular operator in Java that tests whether an object is an instanceof a class

boolean result = obj instanceof Class;
Copy the code

Where obj is an object, Class is a Class or interface, result is true when obj is an object of Class, a direct or indirect subclass of obj, or an implementation of its interface, otherwise false.

Note: The compiler checks to see if obj can be converted to the class type on the right. If it cannot be converted, an error is reported. If the type cannot be determined, the compiler will compile it at runtime.

int i = 0;
System.out.println(i instanceof Integer);  // compile failed, I must be a reference type, not a base type
System.out.println(i instanceof Object); // Failed to compile
Copy the code
Integer integer = new Integer(1);
System.out.println(integer instanceof Integer); // true
Copy the code
System.out.println(null instanceofObject);// false. The JavaSE specification specifies that the instanceof operator returns false if obj is not null
Copy the code

6. Automatic unpacking and packing of Java

  • Boxing automatically converts the base data type to the wrapper type (int–>Integer); Call method: Integer valueOf(int) method

  • Unpacking automatically converts wrapper types to primitive data types (Integer–>int). Call method: intValue method of Integer

Prior to Java SE5, if you wanted to generate an Integer object with a value of 10, you had to do this:

Integer i = new Integer(1);
Copy the code

Autoboxing has been available since Java SE5. If you want to generate an Integer object with a value of 10, you just need to do this:

Integer i = 1;
Copy the code

7. Constant pool in Integer

Example 1: What is the result of the following output

public class Main{
  public static void main(String[] args){
    Integer i1 = 100;
    Integer i2 = 100;
    Integer i3 = 200;
    Integer i4 = 200; System.out.println(i1 == i2); System.out.println(i3 == i4); }}Copy the code

Output result:

true
false
Copy the code

Why did this happen? The output shows that i1 and i2 point to the same object, while i3 and i4 point to different objects. Integer valueOf (); valueOf (); valueOf (); valueOf ()


public static Integer valueOf(int i) {
 if(i >= -128 && i <= IntegerCache.high)
 return IntegerCache.cache[i + 128];
 else return new Integer(i);
}
Copy the code

The IntegerCache class is implemented as follows:

private static class IntegerCache {
 static final int high;
 static final Integer cache[];
 static {
 final int low = -128;
 // high value may be configured by property
 int h = 127;
 if(integerCacheHighPropValue ! =null) {
 // Use Long.decode here to avoid invoking methods that
 // require Integer's autoboxing cache to be initialized
 int i = Long.decode(integerCacheHighPropValue).intValue();
 i = Math.max(i, 127);
 // Maximum array size is Integer.MAX_VALUE
 h = Math.min(i, Integer.MAX_VALUE - -low);
 }
 high = h;
 cache = new Integer[(high - low) + 1];
 int j = low;
 for(int k = 0; k < cache.length; k++)
 cache[k] = new Integer(j++);
 }
 private IntegerCache(a) {}}Copy the code

If the value is between [-128,127], a reference is returned to an existing object in integerCache. cache. Otherwise, a new Integer object is created. The values of i1 and i2 in the above code are 100, so the existing objects are fetched directly from the cache, so i1 and i2 refer to the same object, while i3 and i4 refer to different objects.

Example 2:

public class Main {
 public static void main(String[] args) {
 
 Double i1 = 100.0;
 Double i2 = 100.0;
 Double i3 = 200.0;
 Double i4 = 200.0; System.out.println(i1==i2); System.out.println(i3==i4); }}Copy the code

Running results:

false
false
Copy the code

Reason: The number of integer values in a range is finite, while floating point values are not

8. Tell us the difference between overloading and overwriting

Rewrite (Override)

To rewrite literally means to write again. You’re essentially rewriting the methods that the parent class has in its subclass. Subclass inherits the parent class of the original method, but sometimes a subclass and don’t want to inherit a method in the parent class of intact, so the method name and argument list, a return type (with the exception of a method in a subclass of the return value is the superclass method in a subclass of the return value) are the same, to modify or rewrite the method body, this is to rewrite. However, it is important to note that subclass functions cannot have less access modifier rights than their parent class.

public class Father {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Son s = new Son();
 s.sayHello();
 }
 public void sayHello(a) {
 System.out.println("Hello"); }}class Son extends Father{
 @Override
 public void sayHello(a) {
 // TODO Auto-generated method stub
 System.out.println("hello by "); }}Copy the code

Rewrite summary:

  • Occurs between a parent class and a child class
  • The method name, argument list, and return type (except for a subclass whose method return type is a subclass of its parent) must be the same
  • Access modifiers must be more restrictive than access modifiers for overriding methods (public>protected>default>private)
  • Overriding methods must not throw new check exceptions or check exceptions that are more extensive than those declared by the overriding method

“Overload”

In a class, methods with the same name that have different argument lists (different parameter types, different number of arguments, or even different order of arguments) are considered overloaded. At the same time, an overload has no requirement for the return type. It can be the same or different, but it cannot be judged by whether the return type is the same or not.

public class Father {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Father s = new Father();
 s.sayHello();
 s.sayHello("wintershii");
 }
 public void sayHello(a) {
 System.out.println("Hello");
 }
 public void sayHello(String name) {
 System.out.println("Hello" + ""+ name); }}Copy the code

Reloading summary:

  • Overload is a manifestation of polymorphism in a class
  • Overloading requires that methods with the same name have different argument lists (argument types, number of arguments, and even argument order)
  • When overloaded, the return value type can be the same or different. Overloaded functions cannot be distinguished by return type.

9. Tell me the difference between equals and equals

= =

== compares the (heap) memory addresses of objects stored in variable (stack) memory to determine whether the addresses of two objects are the same, i.e

No means the same object. We are comparing pointer operations in the true sense.

1. We compare whether the operands at both ends of the operator are the same object.

2. Operands on both sides must be of the same type (can be between parent and child classes) to compile.

Int a=10; int a=10; int a=10; int a=10

B =10L and double c=10.0 are the same (true) because they both refer to the heap at address 10.

equals

Equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals The equals method of Object returns a check for equals.

conclusion

  • Equals is used for all comparisons, and constants are written first when comparing because equals objects using object may be null and null.

  • Using only equals in ali’s code specification, ali will default plugin recognition, and can be modified quickly, recommend installing ali plug-in to the old code to use “= =”, replacing the equals.

If you need all Java eight-part articles, click planet for free accessplanet(Github address) If you don’t have a Github partner. You can follow my wechat official account:Java academic lie prone, send the eight-part document, the first time to get all clearance eight-part document. Finally: I wish everyone can get a satisfactory offer, a bright future