Back before the first look at Alibaba often ask JAVA basis you will be a few?

What are the features of the Java language?

  1. Java is a pure object-oriented language. It can directly reflect the objects in real life.
  2. It’s platform independent. Java uses the Java Virtual Machine to run bytecode, compiling Java programs on other platforms, such as Windows, Linux, or MacOS, and allowing the compiled programs to run on other platforms.
  3. Java is an interpreted language. The compiler compiles the Java code into platform-independent intermediate code, and then interprets and runs it on the JVM, which has good portability.
  4. Java provides many built-in class libraries. For example, support for multithreading, network communication support, the most important point is to provide garbage collector.
  5. Java has good security and robustness. Java provides an exception handling and garbage collection mechanism that eliminates the incomprehensible pointer feature in C++.
  6. The Java language provides support for Web application development.

Three features of object orientation?

  1. Inheritance: A new class of an object can derive from an existing class, a derived class can inherit methods and instance variables from its base class, and a derived class can modify or add new methods to better suit specific needs.
  2. Encapsulation: abstract objective things into classes. Each class can operate its data and methods only by trusted classes or objects, and hide information from untrusted ones.
  3. Polymorphism: Allows objects of different classes to respond to the same message. Different objects call the same method even if the parameters are the same, the final performance behavior is different.

Byte-order definitions and what kind of byte-order does Java belong to?

Byte order refers to the order in which multiple bytes of data are stored in computer memory or transmitted over a network. Usually there are two groups of small end and big end.

  1. Small endian: The low level byte is stored at the low end of memory, and the high level byte is stored at the high end of memory.
  2. Big endian: The most significant byte is stored at the lower end of memory, and the least significant byte is stored at the higher end of memory.

The byte order of the Java language is big endian.

What is the difference between a JDK and a JRE?

  1. JDK: Java Development Kit (Java Development Kit), provides the Development environment and running environment of Java.
  2. JRE: Java Runtime Environment (JRE) provides the necessary Environment for Java to run.

The JDK includes the JRE. If you only run Java programs, install the JRE. To write Java programs, you need to install the JDK.

Briefly describe the Java access modifier

  • Default: The default access modifier, visible in the same package
  • Private: Visible within the same class, cannot decorate the class
  • Protected: Visible to classes and all subclasses in the same package. Classes cannot be modified
  • Public: Visible to all classes

Constructor, member variable initialization, and static member variable initialization order?

Sequence: static member variables, member variables, constructor. Detailed order: parent static variable, parent static code block, child static variable, child static code block, parent non-static variable, parent non-static code block, parent constructor, subclass non-static variable, subclass non-static code block, child constructor.

What are the similarities and differences between interfaces and abstract classes?


  1. Can’t be instantiated.
  2. To be instantiated, an implementation class of an interface or a subclass of an abstract class implements the corresponding methods in the interface or abstract class.


  1. An interface can only have method definitions and no method implementations, while an abstract class can have method definitions and implementations.
  2. The keyword for implementing interfaces is implements, and the keyword for inheriting abstract classes is extends. A class can implement more than one interface and can inherit from only one abstract class.
  3. When there is a logical hierarchy between subclasses and superclasses, it is recommended to use abstract classes to facilitate the accumulation of functions. Interfaces are recommended when functionality is not required and you want to support specific interactions between two or more objects that are very different. Using interfaces can reduce the coupling of the software system and facilitate future maintenance or addition and removal of methods.

Why does the Java language not support multiple inheritance?

  1. To make the structure of the program clearer and easier to maintain. Given that the Java language supports multiple inheritance, class C inherits from classes A and B. If class A and B both have custom member methods f(), then calling f() of class C in your code will produce ambiguity. The Java language indirectly supports multiple inheritance by implementing multiple interfaces. Because interfaces only contain method definitions, there can be no method implementation. When class C inherits interfaces A and B, even if they both have method f(), methods cannot be directly called, and specific f() methods can be called only if they are implemented, so there will be no ambiguity.
  2. Multiple inheritance can complicate the sequence of type conversions, constructors, and performance.

The polymorphic mechanism provided by Java?

Java provides two mechanisms for polymorphism, overloading and overriding.

  1. Overloading: Overloading is when you have more than one method of the same name in the same class, but these methods have different arguments so that you can determine which method to call at compile time.
  2. : coverage refers to the derived class to rewrite the base class method, using the base class point to subclass an instance of the object, or the reference variable pointing to the implementation of the interface class instance of the object, in the operation of the program invocation period according to the specific instance object reference variables refers to call of the running of the object method, namely to run time is needed to determine which method calls.

What’s the difference between overloading and overriding?

  1. Overlay is the relationship between the parent class and the child class, which is a vertical relationship. Overloading is a horizontal relationship between methods in the same class.
  2. Overrides can only be related by a single method or a pair of methods; Overloading is a relationship between multiple methods.
  3. Coverage requires the same parameter list; Overloading requires a different parameter list.
  4. In overriding, the body of the calling method is determined by the type of the object, while overloading selects the body of the calling method according to the argument list and the parameter list.
  5. Overloaded methods can change the type of the return value; overriding methods cannot.

What is the difference between final, finally and finalize?

  1. Final is used to declare properties, methods, and classes, indicating, respectively, that properties are immutable, methods are not overridden, and classes are not inheritable.
  2. As part of exception handling, finally can only be used ina try/catch statement. Finally has a statement block used to indicate that the statement must eventually be executed. This is often used when resources need to be released.
  3. Finalize is a method of the Object class that calls the Finalize () method of the collected Object when the garbage collector is executing. When the garbage collector is ready to free the object’s footprint, it first calls the finalize() method and actually collects the object’s footprint the next time the garbage collection occurs.

Does a finally block that appears ina Java program necessarily execute?

Will not be executed when the following conditions are encountered.

  1. The program terminates when an exception occurs before entering the try block.
  2. When a program forces an exit ina try block, such as using System.exit(0), the code in the finally block is not executed.

In other cases, when a try/catch/finally statement is executed, the try block is executed first. When an exception occurs, the catch and finally are processed and the program terminates. When no exception occurs, the code in finally is executed and the code in finally continues. Note that when there is a return ina try/catch block, the code in the finally block executes before the return. If a try/catch/finally block contains a return statement, the return statement in the finally block overrides the return statement in the try/catch block.

What is the function of the keyword static in the Java language?

Static has two main uses:

  1. Allocate a single storage space for a particular data type or object that is independent of the number of objects created.
  2. To associate a method or property with a class rather than an object, that is, to call a method or use a property of a class directly from the class without creating the object.

Specifically, static can be used in four ways:

  1. Modify member variables. A static variable decorated with the static keyword has only one copy in memory. Whenever the class in which the static variable resides is loaded, the static variable is allocated space and can be used with the “” class. Static variables and objects. Static variable ” method used.
  2. Modify member methods. A static modified method can be called without creating an object. Static methods cannot use the keywords this and super. Non-static methods cannot be called. Only static member variables and static member methods of the class can be accessed.
  3. Modify code blocks. The JVM executes static code blocks when it loads a class. Static blocks are often used to initialize static variables. Static blocks are executed only once.
  4. Modify inner classes. Static inner classes can be instantiated independently of external class instance objects. The static inner class cannot have the same name as the outer class. It cannot access ordinary member variables. It can only access static members and static member methods of the outer class.

Java block execution order

  1. Superclass static code block (executed only once)
  2. Subclass static code blocks (executed only once)
  3. The parent class constructs the code block
  4. Superclass constructor
  5. Subclasses construct code blocks
  6. Subclass constructors
  7. Common code block

How do you declare one-dimensional arrays and two-dimensional arrays in Java?

One – dimensional array declarations:

  1. type arrayName[]
  2. type[] arrayName

How to declare a two-dimensional array:

  1. type arrayName[][]
  2. type[][] arrayName
  3. type[] arrayName[]

Type is the basic data type or class, and ArrayName is the arrayName

What’s the difference between String and StringBuffer?

String is an immutable class for String operations. Once a String is created, its value cannot be changed. StringBuffer, on the other hand, is mutable and can still be modified after the object is created.

What is the difference between equals and equals?

== compares references, equals compares content.

  1. If the variable is an underlying data type, == is used to compare whether its corresponding values are equal. If the variable refers to an object,== is used to compare whether two objects refer to the same block of storage.
  2. Equals is one of the methods provided by the Object class. Every Java class inherits from the Object class, so every Object has equals. The equals method defined in the Object class calls the == comparison Object directly internally. But the overriding method allows it to compare data content rather than references.

    Why design String as an invariant?

  3. Space Savings: String constants stored in the JVM’s string pool can be shared by users.
  4. Improved efficiency: Strings are shared by different threads and are thread-safe. Synchronization is not required in operations involving multiple threads.
  5. Security: String is commonly used for user names, passwords, file names, etc. Since it is immutable, it can avoid malicious modification by hackers.

    What is serialization?

    Serialization is the process of converting an object into a sequence of bytes to solve problems that arise when reading or writing to a stream of objects. Serialization can write the state of an object to a stream for network transmission, or save it to a file, database, or other system, and then read the stream and reconstruct it into an identical object when needed.

Briefly describe the Class object in Java

Objects in Java can be divided into instance objects and Class objects. Each Class has a Class object that contains information about that Class.

How to get a Class object:

  • Class.forName(” fully qualified name of the Class “)
  • Instance Object.getClass()
  • The name of the class. The class

What is the Java reflection mechanism?

JAVA reflection mechanism means that in the process of program running, you can construct the object of any class, get the member variables and member methods of any class, get the class information of any object, and call the properties and methods of any object. Reflection mechanism enables Java to dynamically obtain program information and dynamically call object methods. The reflection API can be invoked with the following classes.

  • Class: Obtain Class attribute methods
  • Field class: Gets the member variables of the class
  • Method class: Gets the Method information for the class
  • The Construct class: Gets information about the class’s constructor, etc

The paper notes

Java annotations are used to provide metadata for Java code. As metadata, annotations do not directly affect the execution of your code, but there are some types of annotations that can actually be used for this purpose.

It can be used to provide information to the compiler, in the compilation stage to provide information to the software for related processing, in the run time processing to write the corresponding code, do the corresponding operation.

Brief element comment

Meta annotations can be understood as annotations of annotations, that is, used within annotations to achieve the desired functionality. It is specifically divided into:

  • Retention: Indicates whether the annotation exists in source code, bytecode (classloading), or runtime (running in the JVM).
  • @target: Indicates the scope of the effect of the annotation.
  • @Documented: Include the element from the annotation into the Javadoc.
  • @inherited: An annotation that is annotated by @inherited modifies a parent class. If its children are not annotated by other annotations, their children inherit the parent’s annotations.
  • @repeatable: An annotation decorated with this meta annotation can be applied to an object multiple times, but each time the annotation is applied it can represent a different meaning.

Describe the classification of Java exceptions

Java exceptions can be classified as Error (errors that the program cannot handle), and Exception (exceptions that the program itself can handle). Both classes inherit Throwable.

The Error common StackOverFlowError, OutOfMemoryError and so on.

Exception can be divided into run-time and non-run-time exceptions. Runtime exceptions can be handled using a try catch or not. Non-runtime exceptions must be handled, and the program will not compile without handling them.

Describe the difference between throw and throws

Throws are typically used inside the body of a method and are defined by the developer to proactively throw an exception when a program statement has a problem.

Throws is typically used on a method declaration, representing a list of exceptions the method may throw.

Briefly describes the generic

Generics, or “parameterized types,” solve the problem of uncertain object types. Valid at compile time. In generic usage, the data type of the operation is specified as a parameter, which is referred to as a generic class in a class, a generic interface in an interface, and a generic method in a method.

Briefly describe generic erasure

The bytecode generated by the Java compiler does not contain generic information. Generic type information is erased during compilation, a process called generic erasure.

Describe the basic Java data types

  • Byte: 1 byte, value range -128 to 127
  • Short: takes 2 bytes and ranges from -2^15^ to 2^15^-1
  • Int (2^31^ to 2^31^-1) int (2^31^-1
  • Long: 8 bytes
  • Float: Occupies 4 bytes
  • Double: takes 8 bytes
  • Char: Occupies 2 bytes
  • Boolean: Occupation size varies depending on the implementation of the virtual machine

    Brief description of automatic packing and unpacking

    For Java base data types, there is a wrapper class.

Boxing is the automatic conversion of basic data types to wrapper types, such as Int-> Integer

Unboxing is the automatic conversion of a wrapper type to a primitive data type, such as Integer->int

Describe the difference between overloading and overriding

Overriding means that a subclass overrides a method of its parent class. The corresponding parameter and return value types of the method cannot be changed.

Overloading is when the method name is the same but the parameter type or number is different within a class.

Briefly describe the polymorphism of Java

Java polymorphism can be divided into compile-time polymorphism and run-time polymorphism.

Compile-time polymorphism mainly refers to the overloading of methods, that is, the distinction between different methods by different argument lists.

Runtime polymorphism mainly refers to the use of a parent reference to a subclass object when inheriting from the parent class and implementing an interface.

The implementation of runtime polymorphism: mainly depends on the method table, which stores the methods of the Object class first, the methods of the superclass of the class next, and the methods of the class itself last. If a subclass overwrites a method of the superclass, then the subclass shares a method table entry with those methods of the same name in the superclass and is considered to be the method of the superclass. Thus, runtime polymorphism can be implemented.

Describe the difference between abstract classes and interfaces

Abstract class: reflects the is-a relationship, such as man is a person, we can define Person as an abstract class.

Interface: Represents the relation of CAN. Is implemented as a template. If you set the interface Fly, Plane class and Bird class can all realize this interface.

A class can inherit from only one abstract class, but can implement multiple interfaces.

Describe the difference between == and equals

In the case of ==, the corresponding value is compared in the case of the base data type, and in the case of the reference data type, the location of its memory is compared.

The equals method is the same as == when it is not overridden, but the user can modify the logic according to the corresponding requirement. For example, if a property value of an object is the same, it will return true if it is the same, and false if it is different. Make sure the equals method is the same and the corresponding object, hashCode, is the same.

Describe the common methods of the Object class

  1. HashCode: The hashCode computed from the object. Used with the map or equals method. You need to ensure that the method is called multiple times on the same object and always returns the same integer value.
  2. Equals: Determines whether two objects are consistent. Make sure the equals method is the same and the corresponding object, hashCode, is the same.
  3. ToString: Represents the object as a string
  4. Clone: Deep copy an object

Describe inner classes and what they do

  • Member inner class: An inner class that acts as a member object. You can access the properties and methods of private and above external classes. When an outer class wants to access an inner class property or method, it must create an inner class object and then access the inner class property or method through that object. External classes also have access to the inner class properties of the private decoration.
  • Local inner class: An inner class that exists in a method. Access permissions are similar to local variables in that you can only access final variables of the external class.
  • Anonymous inner class: Can only be used once, has no class name, and can only access final variables of the outer class.
  • Static inner class: A class – like static member variable.

Briefly describe String/StringBuffer and StringBuilder

The String class uses a final modified character array for String storage and is therefore immutable. If you are modifying a String, you need to create a new object that contains both the old and the new characters.

StringBuilder, which is saved as an array of characters without final modifiers, is mutable. But it’s not thread safe.

StringBuffer, which is stored in an array of characters without final modification, can be understood as a thread-safe StringBuilder.

Describe the implementation of serialization and deserialization in Java

Serialization: Converts a Java object into a sequence of bytes that can be transferred through a network object.

Deserialization: Converts a sequence of bytes to a Java object.

Implementation: Implement the Serializable interface, or implement the writeExternal() and readExternal() methods in the Externalizable interface.

Briefly describe the Java List

List is an ordered queue, which can be implemented in two ways in Java:

ArrayList is a non-thread-safe list with variable capacity. It is fast for random access. When the collection expands, it creates a larger array and copies the existing array into the new array.

LinkedList is essentially a bidirectional LinkedList, which is faster to insert and delete than an ArrayList, but slow to access random elements.

What are the basic thread-safe data structures in Java

HashTable: Hashtable Vector: thread-safe ArrayList: thread-safe Stack: BlockingQueue and its subclasses: thread-safe Queue ConcurrentHashMap: a thread-safe alternative to Hashtable Vector: thread-safe ArrayList: thread-safe Stack: BlockingQueue and its subclasses: thread-safe Queue

Briefly describe the Java Set

A Set is a data structure that does not allow elements to be duplicated and unordered. Java implements sets in three ways:

HashSet is implemented through HashMap, the Key of the HashMap is the element stored in the HashSet, and the Value system defines an Object-type constant named Present. < hashCode > < hashCode > < hashCode > < Equals > < hashCode > < Equals > > < O(1) > < hashCode > < Equals > >

A LinkedHashSet inherits from a HashSet and is implemented through a LinkedHashMap, which maintains element insertion order using a bidirectional linked list.

TreeSet is implemented by TreeMap. The underlying data structure is a red-black tree. When adding elements to the collection, it will be inserted into the appropriate position according to the comparison rules to ensure that the collection after insertion is still ordered. Query O (logn)

Briefly describe the Java HashMap

Prior to JDK8, the underlying implementation was array + linked list. JDK8 changed to array + linked list/red-black tree. The main member variables include the table array to store the data, the number of elements size, and the loadFactor. The data in a HashMap is in the form of key-value pairs, whose hash values are used to calculate the index of the array. If two key elements have the same hash value, there will be a hash collision and they will be placed on the same linked list.

The Node/Entry Node contains four member variables: the key, the value, the next pointer, and the hash value. After JDK8, a list exceeding 8 will be converted to a red-black tree.

If the current data/total data capacity is > load factor, HashMap will perform capacity expansion. The default initialization capacity is 16, the expansion capacity must be a power of 2, the maximum capacity is 1<< 30, and the default load factor is 0.75.

Why are HashMap threads unsafe

In JDK1.7, HashMap uses header interpolation to insert elements, resulting in a circular linked list in concurrency, resulting in an infinite loop.

Although JDK1.8 uses tail interpolation to solve this problem, the concurrent PUT operation also causes the previous key to be overwritten by the next key.

Since HashMap has capacity expansion mechanism, there are also cases where thread A has capacity expansion and thread B fails to execute the GET method.

Briefly describe the Java TreeMap

TreeMap is a Map structure realized by the red-black tree at the bottom level. The bottom implementation is a balanced sorting binary tree. Because the complexity between the red-black tree’s insertion, deletion and traversal is O(logN), the performance is lower than that of the hash table. However, the hash table cannot provide the ordered output of key value pairs. The red-black tree can output the ordered output according to the size of the key value.

What’s the difference between Collections and Collections?

  1. Collection is a Collection interface that provides common interface methods for basic operations on Collection objects. All collections are subclasses of it, such as List, Set, and so on.
  2. Collections is a wrapper class that contains many static methods that cannot be instantiated, but rather is used as a utility class, such as the provided sorting methods: Collections.sort(list); Reverse method provided: Collections.reverse(list).

What are the similarities and differences between ArrayList, Vector and LinkedList?

  1. ArrayList, Vector, and LinkedList are all scalable arrays, that is, arrays whose length can be changed dynamically.
  2. ArrayList and Vector are based on an Object[] Array that stores elements. They create a contiguous space in memory for storage and support subscript and index access. However, when it comes to inserting elements, it may need to move the elements in the container, so the insertion efficiency is low. Both the ArrayList and the Vector expand when the storage elements exceed the initial capacity of the container.
  3. Vector is thread-safe, and most of its methods are synchronized directly or indirectly. ArrayList is not thread safe and its methods are not synchronous. LinkedList is also not thread safe.
  4. LinkedList is implemented by two-way list, and the data index needs to be traversed from the beginning, so the random access efficiency is low. However, there is no need to move the data when inserting elements, so the insertion efficiency is high.

What’s the difference between a HashMap and a Hashtable?

  1. HashMap is a lightweight implementation of Hashtable that allows NULL keys and values, but allows NULL keys for up to one record. Hashtable doesn’t.
  2. The methods in a Hashtable are thread-safe, while a HashMap is not. Accessing a HashMap in multiple threads requires additional synchronization mechanisms.
  3. Hashtable is traversed using Enumeration and HashMap is traversed using Iterator.

How do you decide whether to use a HashMap or a TreeMap?

HashMap is a better choice if the Map is inserted, deleted, or positioned with an element more frequently. TreeMap is a better choice if you need an ordered traversal of the Key collection.

What is the difference between fail-fast and fail-safe iterators?

  1. Fail – fast on the container directly, in the process of traversal, once found in a container data is modified, immediately throw ConcurrentModificationException leading to traverse the failure. Common examples of containers that use Fail-fast are HashMap and ArrayList.
  2. The fail-safe traversal is based on a clone of the container. So changes to the contents of the container do not affect the traversal. Common fail-safe containers are ConcurrentHashMap and CopyOnWriteArrayList.

What is the relationship between equals and hashCode in a HashSet?

Equals and hashCode are both inherited from the Object class. Equals determines whether an object’s memory address is the same address. HashCode converts the memory address of an object into a hashCode according to the defined hash rules. HashCode and equals are two methods used to determine whether the objects stored in a hashSet are the same:

  1. If two objects have different hashCode values, the two objects are not the same.
  2. If two objects have the same hashCode value, then the object’s equals method is called. If the equlas method returns true, then the two objects are the same; otherwise, they are not.