I believe that many of you have encountered a problem in the interview this year. You have been brushing the advanced project framework while forgetting the basic content that you should not forget.

I never thought of it, but finally fell on the basis of the above, below for you to prepare some basic project questions, you can check the missing.

1. The difference between object-oriented and procedural

Process oriented

Advantages: Higher performance than object-oriented, because class invocation needs to be instantiated, the overhead is relatively large, more consumption of resources; For example, SCM, embedded development,

Linux/Unix and others generally adopt process oriented development where performance is the most important factor.

Disadvantages: No object-oriented easy maintenance, easy reuse, easy to expand

object-oriented

Advantages: easy to maintain, easy to reuse, easy to expand, because of the characteristics of object-oriented encapsulation, inheritance, polymorphism, can design a low coupling system, so that the system

The system is more flexible and easier to maintain

Disadvantages: Lower performance than process-oriented

2. What are the features of the Java language

1. Simple and easy to learn;

2. Object-oriented (encapsulation, inheritance, polymorphism);

3. Platform independence (Java VIRTUAL machine implementation platform independence);

4. Reliability;

5. Safety;

6 support multithreading (C++ language has no built-in multithreading mechanism, so must call the operating system multithreading function to carry out multithreading program design, and

The Java language provides multithreading support);

7. Support network programming and very convenient (The Java language itself was designed to simplify network programming, so the Java language not only support network programming and

Very convenient);

8. Compile and interpret simultaneously;

JVM JDK and JRE

JVM

A Java Virtual machine (JVM) is a virtual machine that runs Java bytecode. JVMS have specific implementations for different systems (Windows, Linux,

MacOS).

Purpose: To use the same bytecode, they all give the same result.

What is bytecode? What are the benefits of adopting bytecode?

In Java, code that can be understood by the JVM is called bytecodes (files with a.class extension) that are not oriented to any particular processor, only to

The virtual machine.

Java eliminates the low efficiency of traditional interpreted languages to some extent by means of bytecode, while preserving the portability of interpreted languages

Plant characteristics.

So Java programs run more efficiently, and because bytecode is not specific to a particular machine, Java programs can run without recompiling

Running on a variety of different computers.

Java programs from source to run generally have the following three steps:

We need to pay special attention to the.class-> machine code step.

In this step, the JVM classloader loads the bytecode file and then executes it line by line through the interpreter, which is relatively slow.

In addition, some methods and blocks of code need to be called frequently, so called hot code, so later introduced the JIT compiler, JIT belongs to the runtime

Compilation.

When the JIT compiler completes the first compilation, it saves the machine code corresponding to the bytecode for direct use next time.

And we know that machine code is certainly more efficient than Java interpreters. This also explains why we often say that Java is compilation and interpretation

In the language.

HotSpot adopts Lazy Evaluation where, according to the 80/20 rule, only a small percentage of code consumes most system resources (hot

Point code), which is what the JIT needs to compile.

The JVM collects information based on each time the code is executed and makes some optimizations accordingly, so the more times it executes, the faster it gets.

JDK 9 introduces a new compilation mode

AOT(Ahead of Time Compilation), which compiles bytecode directly to machine code, thus avoiding JIT warm-up and other overhead. JDK

Support for hierarchical compilation and AOT collaboration.

However, AOT compilers are certainly not as good as JIT compilers.

Summary: A Java Virtual machine (JVM) is a virtual machine that runs Java bytecode. JVMS have specific implementations for different systems (Windows, Linux,

MacOS), the purpose is to use the same bytecode, and they all give the same result.

Bytecode and JVM implementations for different systems are key to the Java language’s “compile once, run anywhere” approach.

The JDK and JRE

The JDK is the Java Development Kit, which is a full-featured Java SDK. It has everything the JRE has, plus a compiler (Javac) and tools

(such as Javadoc and JDB). It can create and compile programs.

JRE is the Java runtime environment. It is a collection of everything you need to run a compiled Java program, including the Java Virtual Machine (JVM), And Java classes

Libraries, Java commands, and other basic artifacts.

However, it cannot be used to create new programs.

If you just want to run a Java program, you just need to install the JRE.

If you need to do some Java programming, you’ll need to install the JDK.

However, this is not absolute. Sometimes, even if you don’t plan on doing any Java development on your computer, you still need to install the JDK.

For example, if you deploy a Web application using JSPS, you are technically just running Java programs in the application server. So why do you need the JDK?

Because the application server converts JSPS into Java servlets, you need to use JDK to compile the servlets.

4. Oracle JDK vs. OpenJDK

There are probably a lot of people like me who haven’t touched or used OpenJDK before looking at this question. So are there any major differences between Oracle and OpenJDK?

Here are some information I collected to answer this question that many people ignore.

For Java 7, there is nothing critical.

The OpenJDK project is based on HotSpot source code donated by Sun. In addition, OpenJDK was selected as the reference implementation for Java 7 by Oracle engineers

Maintenance.

About the differences between the JVM, JDK, JRE and OpenJDK.

A more detailed answer:

Q: What is the difference between the source code in the OpenJDK repository and the code used to build the Oracle JDK?

A: Very close – our Oracle JDK version build process is based on OpenJDK 7 and only adds a few parts, such as deployment code, which includes packages

Includes Oracle’s Java plug-in and Java WebStart implementation, as well as some closed source party components, such as graphical rasterizers, and some open source first

Tripartite components, such as Rhino, and bits and pieces, such as attached documentation or third-party fonts.

Going forward, our goal is to open source all parts of the Oracle JDK, except the parts where we consider commercial functionality.

Conclusion:

1. Oracle JDK releases will be released every three years, while OpenJDK releases will be released every three months;

2. OpenJDK is a reference model and is fully open source, while Oracle JDK is an implementation of OpenJDK and is not fully open source;

3. Oracle JDK is more stable than OpenJDK. The OpenJDK and Oracle JDK have nearly the same code, but the Oracle JDK has more classes and some bugs

Repair by mistake.

Therefore, if you want to develop enterprise/business software, I recommend you choose the Oracle JDK because it is thoroughly tested and stable.

In some cases, some people have mentioned that they may experience a lot of application crashes when using OpenJDK, but simply switch to the Oracle JDK

Can solve the problem;

4. Top companies are using the Oracle JDK, such as Android Studio, Minecraft and IntelliJ IDEA development tools, where the Open JDK is less

Popular;

5. Oracle JDK provides better performance than OpenJDK in terms of responsiveness and JVM performance;

6. The Oracle JDK does not provide long-term support for upcoming releases. Users must update to the latest version each time to get support.

7. Oracle JDK is licensed under the binary code License, while OpenJDK is licensed under the GPLv2 license.

5. The difference between Java and C++

I know many people have never learned C++, but the interviewer likes to compare Java with C++. No way!! Even if you haven’t learned C++, write it down!

  • Both are object-oriented languages that support encapsulation, inheritance, and polymorphism

  • Java does not provide Pointers to access memory directly; program memory is more secure

  • Java classes are single inheritance, C++ supports multiple inheritance; Although Java classes cannot be multiinherited, interfaces can.

  • Java has automatic memory management mechanisms that do not require programmers to manually free unwanted memory

6. What is the main class of a Java program what is the difference between the main class of an application and that of a small program

A program can have more than one class, but only one class can be the main class.

In A Java application, this main class is the class that contains the main () method. In Java applets, the main class is an inherited system class

JApplet or a subclass of Applet.

The main class of the application is not required to be public, but the main class of the applet must be public. The main class is the entry point for Java program execution.

7. What are the differences between Java applications and applets

Simply put, the application starts from the main thread (the main() method).

Applets do not have a main method, but instead run embedded in a browser page (called init() thread or run() to start)

Flash is similar to small games.

8. Difference between character constants and string constants

1. Form: A character constant is a single character caused by a single quotation mark. A string constant is several characters caused by a double quotation mark

2. Meaning: character constant is equivalent to an integer value (ASCII value), can participate in expression operation string constant represents an address value (the string is in memory

Storage location)

3. Memory size Character constant takes up only 2 bytes A string constant takes up several bytes (at least one end-of-character flag) (note: char takes up two bytes in Java

Bytes)

9. Whether Constructor Constructor can be overridden

When we talk about inheritance, we know that the private properties and constructors of the parent class cannot be inherited, so Constructor cannot be overridden.

But you can overload, so you can see multiple constructors in a class.

10. Difference between overloading and overwriting

Overloading: occurs in the same class, the method name must be the same, the parameter type is different, the number is different, the order is different, the method return value and access modifier can

The difference occurs at compile time.

Overwrite: occurs when a parent class has the same method name, argument list, return value range is less than or equal to the parent class, and throw exception range is less than or equal to the parent class

Class, whose access modifier range is greater than or equal to the parent class;

If the superclass method access modifier is private, the subclass cannot override the method.

11. Three major features of Java object-oriented programming: encapsulation inheritance polymorphism

encapsulation

Encapsulation privatized the properties of an object while providing methods for properties that can be accessed by the outside world

Methods must be provided for external access. But if a class has no methods to access from the outside world, then that class is meaningless.

inheritance

Inheritance is the technique of creating a new class from the definition of an existing class, either by adding new data or functionality, or by using the definition of a parent class

Function, but cannot selectively inherit from the parent class. Using inheritance makes it very easy to reuse old code.

Here are three things to remember about inheritance:

1. Subclasses have non-private properties and methods of their parent class.

2. Subclasses can have their own attributes and methods, that is, subclasses can extend their parent classes.

3. Subclasses can implement methods of their parent class in their own way. (More on that later).

polymorphism

Polymorphism refers to the fact that the specific type to which a reference variable is defined in a program and the method calls made through that reference variable are not determined at programming time, but rather

It is only determined during program execution that a reference variable actually points to an instance object of a class and in which class the method call from that reference variable is actually made

The method of implementation must be determined during program execution.

Polymorphism can be implemented in Java in two forms: inheritance (overwriting the same method by multiple subclasses) and interfaces (implementing the interface and overwriting the same method in the interface).

12. The difference between StringBuffer and StringBuilder

What is String and why is String immutable

Variability can be put simply:

The String class uses final keyword character arrays to hold strings, privateFinal char value[].

So strings are immutable.

And StringBuilder and StringBuffer both inherit from AbstractStringBuilder, AbstractStringBuilder

The string char[]value is also held ina character array but is not modified with the final keyword, so both objects are mutable.

Both StringBuilder and StringBuffer constructors are implemented by calling the superclass constructor, AbstractStringBuilder.

You can consult the source code:


abstract class AbstractStringBuilder implements Appendable, CharSequence {

char\[\] value;

int count;

AbstractStringBuilder() {

}

AbstractStringBuilder(int capacity) {

value \= new char\[capacity\];

}Copy the code

Thread safety

Objects in strings are immutable, which means they are considered constants, thread-safe.

AbstractStringBuilder AbstractStringBuilder is a common parent of StringBuilder and StringBuffer that defines some basic string operations.

Public methods such as expandCapacity, Append, INSERT, indexOf, etc.

StringBuffer places synchronization locks on methods or on invoked methods, so it is thread-safe. StringBuilder does not add to methods

Synchronous locking, so it’s not thread-safe.

performance

Each time a String is changed, a new String is generated, and the

Pointer to a new String. A StringBuffer operates on the StringBuffer object itself each time, rather than generating new objects and changing object references.

Using StringBuilder gives only a 10% to 15% improvement over using StringBuffer in the same situation, but at the risk of multithreading insecurity

Risk.

Summary of the use of the three:

1. Operate on a small amount of data = String

2. Single thread operation on large amount of data in string buffer = StringBuilder

3. Multithreaded manipulation of large amounts of data in string buffers = StringBuffer

13. Automatic packing and unpacking

Boxing: Wrapping base types with their corresponding reference types;

Unpacking: convert packaging type to basic data type;

14. Why it is illegal to call a non-static member in a static method.

Because static methods can be called without an object, no other non can be called in a static method

Static variables are also not accessible to non-static variable members.

15. Define a construct in Java that does nothing and takes no parameters

Function of method

If a Java program calls a superclass-specific constructor without super() before executing a subclass constructor, it will call the “no arguments” constructor of the superclass

Construction method “.

Therefore, if only the constructor with parameters is defined in the parent class, and no super() is used in the subclass constructor to call the specific constructor in the parent class

Method, an error will occur at compile time because the Java program cannot find a constructor in its parent class that has no arguments to execute. The solution is to add one to the parent class

A constructor that does nothing and takes no arguments.

What is the difference between import Java and Javax

At the beginning, the packages required for Java apis were Java-starting packages, and Javax was only used to extend API packages.

Over time, however, Javax has expanded to become part of the Java API.

However, moving extensions from the Javax package to the Java package would be too cumbersome and would end up breaking a bunch of existing code. Therefore, it was ultimately decided that the Javax package would become

Part of the standard API. So, there’s really no difference between Java and Javax. It’s all one name.

What is the difference between an interface and an abstract class

1. The default interface method is public, all methods can not have a default implementation in the interface (Java 8 interface methods can have a default implementation), abstract classes can have

Non-abstract methods.

2. Instance variables in interfaces are final by default, but not necessarily in abstract classes

3. A class can implement multiple interfaces, but can only implement one abstract class at most

4. A class that implements an interface must implement all the methods of the interface, not necessarily an abstract class

5. An interface cannot be instantiated with new, but can be declared, but must refer to an object that implements the interface

Image is a template design, interface is an abstraction of behavior, is a specification of behavior.

18. What are the differences between member variables and local variables

1. Syntactically, member variables belong to classes, while local variables are variables defined in methods or parameters of methods; A member variable can be

Public,private,static, etc. Local variables cannot be modified by access-control modifiers or static. However, member variables and local variables

Can be modified by final;

2. From the storage mode of variables in memory, member variables are part of objects, while objects exist in heap memory and local variables exist in stack memory 3. From the change

In terms of the in-memory lifetime of a quantity, a member variable is a part of an object that exists with the creation of the object, while a local variable is created with the invocation of a method

Move away.

3. If a member variable is not assigned an initial value, it is automatically assigned to the default value of the type (except that final member variables must also be displayed)

Assignment); Local variables are not automatically assigned.

19. What operator is used to create an object? How is an object entity different from an object reference?

The new operator creates the object instance (which is in heap memory) and the object reference points to the object instance (which is in stack memory). one

Object references can point to zero or one object (a string can be tied to either a balloon or a balloon); An object can have n references to it

It can be tied to a balloon with n strings.

20. What is the return value of a method? What is the function of a return value in a class method?

The return value of a method is the result of executing code in a method body. (provided that the method is likely to produce results).

Return value: Receives the result so that it can be used for other operations.

21. What does a class constructor do if a class does not declare a constructor, does the program execute correctly? Why is that?

The main function is to complete the initialization of the class object. Yes.

Because a class will have a default constructor that takes no arguments even if it does not declare a constructor.

22. What are the properties of the constructor

1. The name is the same as the class name.

Void (); void (); void ();

3. It is automatically executed when objects of the class are generated.

23. What is the difference between static and instance methods

1. You can use the “class name” when calling static methods externally. Method name, or object name. Method name “. Instance methods have only the last part

This way. That is, you can call a static method without creating an object.

2. Static methods only allow access to static members (that is, static member variables) when accessing members of this class

And static methods) without allowing access to instance member variables and instance methods; Instance methods do not have this limitation.

24. What is the difference between equality of objects and equality of references to them?

The equality of objects compares whether the contents in memory are equal. But by quoting equality, you’re comparing them

Whether the memory addresses pointed to are equal.

The subclass constructor with no arguments is called before the subclass constructor is called.

Help subclasses do initialization work.

26. == equals(important)

== : This is used to determine whether the addresses of two objects are equal. That is, determine whether two objects are the same object. The base data type == is compared

Value, reference data type == compares memory addresses.

*equals() : * Determines whether two objects are equal.

But it is generally used in two ways:

Case 1: The class does not override equals(). Comparing two objects of that class through equals() is equivalent to comparing them through “==”.

Case 2: The class overrides equals(). In general, we override equals() to make the contents of two objects equal; If their contents are equal, then

Return true (that is, the two objects are considered equal).

Here’s an example:

public static void main(String\[\] args) { String a \= new String("ab"); // a is a reference to String b \= new String("ab"); String aa \= "ab"; String bb \= "ab"; If (aa \== bb) // true system.out.println ("aa==bb"); If (a \== b) // false, not the same object system.out.println ("a==b"); if (a.equals(b)) // true System.out.println("aEQb"); If (42 \== 42.0) {// true system.out.println ("true"); }}}Copy the code

Description:

  • The equals method on String is overridden, because the equals method on object compares the memory addresses of objects, while the equals method on String compares the values of objects.

  • When creating an object of type String, the virtual machine looks in the constant pool for an existing object with the same value as the object to be created, and assigns it to the current reference if it does. If not, create a new String in the constant pool.

An interviewer might ask you, “Have you overridden hashCode and equals, and why

Must override the hashCode method?

Introduction to hashCode ()

HashCode () is used to get a hashCode, also known as a hashCode; It actually returns an int.

The purpose of this hash code is to determine the index position of the object in the hash table. HashCode () is defined in the JDK’s Object.java, which means that

Any class in Java contains the hashCode() function.

Hash table stores key-value pairs. It can quickly retrieve the corresponding value according to the key. That’s where the hash code comes in!

(Can quickly find the desired object)

Why hashCode

Let’s use “how a HashSet checks for duplicates” as an example of why hashCode is needed:

When you add an object to a HashSet, the HashSet evaluates the object’s Hashcode value to determine where the object is added to, as well as to what other objects have been added to

If there is no matching HashCode, the HashSet assumes that the object has not been repeated.

But if objects with the same Hashcode value are found, the equals () method is called to check whether objects with hashCode equality are really the same.

If they are the same, the HashSet will not let the join succeed.

If it is different, it will be rehashed to another location.

This significantly reduced the number of equals calls, which in turn significantly increased the execution speed.

HashCode () and equals ().

1. If two objects are equal, the Hashcode must also be the same

2. If two objects are equal, calling equals returns true on both objects

3. If two objects have the same hashCode value, they are not necessarily equal

4. Therefore, if equals is overridden, hashCode must be overridden as well

5. The default behavior of hashCode() is to generate unique values for objects on the heap. If hashCode() is not overridden, then the class’s two objects anyway

Will not be equal (even if the two objects point to the same data)

28. Why is there only value passing in Java

Why is there only value passing in Java?

29. Describe the basic concepts of threads, programs, and processes. And what is the relationship between them?

A thread is similar to a process, but a thread is a smaller unit of execution than a process. A process can produce multiple threads during its execution.

Unlike a process, multiple threads of the same kind share the same memory space and a set of system resources, so the system is producing a thread, or between threads

Threads are also called lightweight processes because they are much less burdensome than processes when switching between tasks.

A program is a file containing instructions and data that is stored on a disk or other data storage device, that is, a program is static code.

Process is a process of program execution, is the basic unit of the system running program, so the process is dynamic.

When a system runs a program, it is a process from creation, to execution, to extinction. Simply put, a process is an executing program that computes

The machine executes instruction after instruction, and each process occupies certain system resources such as CPU time, memory space, files, files, and inputs

Access to input and output devices and so on. In other words, when a program is executed, it is loaded into memory by the operating system.

Threads are smaller running units that processes are divided into. The big difference between threads and processes is that basically each process is independent, whereas each thread is not, because

It is highly likely that threads in the same process will affect each other.

On the other hand, processes belong to the category of operating system, mainly in the same time, can execute more than one program, while threads are in the same

The execution of more than one segment of a program almost simultaneously.

30. What are the basic states of threads?

A Java thread can only be in one of six different states at a given time in its running life.

Threads are not fixed in one state during their life cycle but switch between states as the code executes.

Java thread state transitions are shown below:

31 Summary of the final keyword.

The final keyword is used in three main places: variables, methods, and classes.

1. For a final variable, if it is of a basic data type, its value cannot be changed once initialized; If it is a reference type change

After initialization, it cannot be made to point to another object.

2. When you modify a class with final, it indicates that the class cannot be inherited. All member methods ina final class are implicitly specified as final methods.

3. There are two reasons for using the final method.

The first reason is to lock the method in case any inherited classes change its meaning.

The second reason is efficiency. In earlier Versions of Java implementations, final methods were converted to inline calls.

But if the method is too large, you may not see any performance gains from inline calls (the current Version of Java does not require final methods to do so

These are optimized). All private methods ina class are implicitly specified as final.

32 Exception Handling in Java Java exception class hierarchy diagram

In Java, all exceptions have a common ancestor to the Throwable class in the java.lang package.

Throwable has two important subclasses: Exception and Error.

Both are important subclasses of Java exception handling, each containing a large number of subclasses.

Error: An Error that the program cannot handle, indicating a serious problem in running the application.

Most errors have nothing to do with what the code writer is doing, and instead represent problems with the JVM (Java Virtual Machine) while the code is running. For example, the Java Virtual machine

Virtual MachineError, an OutOfMemoryError occurs when the JVM no longer has the memory resources it needs to continue with the operation.

When these exceptions occur, the Java Virtual Machine (JVM) typically selects thread termination.

These errors indicate that the fault occurred in the Virtual machine itself or when the Virtual machine was trying to execute an application, such as a Java Virtual machine running error (Virtual)

MachineError), class definition error (NoClassDefFoundError), etc.

These errors are not detectable because they are outside the control and processing capabilities of the application, and most of them are not allowed to occur while the program is running.

Even if an error does occur, a properly designed application should not, by nature, attempt to handle the exception it raises.

In Java, errors are described by subclasses of Error.

Exception: An Exception that can be handled by the program itself. The Exception class has an important subclass, RuntimeException.

RuntimeException is thrown by the Java VIRTUAL machine.

NullPointerException (thrown when the variable to be accessed does not reference any object) and ArithmeticException (ArithmeticException, 1)

An integer divided by zero, put the exception) and ArrayIndexOutOfBoundsException (subscript cross-border exception).

Note the difference between exceptions and errors:

Exceptions can be handled by the program itself, but errors cannot.

Common Throwable class methods:

  • Public string getMessage(): Returns details about when an exception occurs

  • Public string toString(): Returns a brief description of when an exception occurs

  • Public string getLocalizedMessage(): Returns localization information for the exception object.

Overriding this method with a subclass of Throwable can claim localized information.

If the subclass does not override the method, the method returns the same information as getMessage ()

Public void printStackTrace(): Prints the Throwable object encapsulated on the console

Exception information

Summary of Exception Handling

** Try block: ** used to catch exceptions. This may be followed by zero or more catch blocks, or if there is no catch block, a finally block must follow.

Catch block: Used to handle exceptions caught by a try.  Finally Blocks: Statements in the finally block are executed regardless of whether exceptions are caught or handled.

When a return statement is encountered ina try or catch block, the finally block is executed before the method returns.

A finally block is not executed in four special cases:

1. An exception occurred in the finally statement block.

2. Use system.exit () in the previous code to exit the program.

3. The thread where the program resides dies.

4. Shut down the CPU.

33 What if some fields in Java serialization do not want to be serialized?

For variables that do not want to be serialized, use the TRANSIENT keyword modifier.

The transient keyword prevents serialization of variables in an instance that are modified with this keyword.

When an object is deserialized, variable values modified transient are not persisted and restored.

Transient can only modify variables, not classes and methods.

34 Get two common methods of keyboard input.

Method 1: Use Scanner

` ` ` ` ` ` ` `

Scanner input = new Scanner(System.in);

String s = input.nextLine();

input.close()““““

Method 2: Through BufferedReader

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

String s = input.readLine();

The above interview questions have been compiled into a PDF file, hoping to help you review and find a good job before the interview, and relatively save you time searching for information on the Internet to learn!!

Attached welcome to pay attention to my public variety number: IT resources of the home, scan the following two-dimensional code can receive more first-line large factory Java interview materials!

The last

Welcome to the comments section to communicate and improve each other; Sorting out information is not easy, if you like the article remember to point a zan ha, thank you for your support!!