java interview questions (basic)

java overview

1. What is programming?

The computer uses a programming language to write codes to solve a certain problem, and the process of obtaining the result.

2. What is java?

Java is an object-oriented programming language with powerful functions and easy-to-use features.

3.jdk、jre、jvm

jdk: java development tools (including jre)

jre: java operating environment (including jvm and core class library)

jvm: java virtual machine

4. What is cross-platform? What is the principle?

Cross-platform: Compile once and run on multiple systems

Principle: As long as the corresponding java virtual machine is installed in the system, the system can run java programs

5. Java language features

easy to learn

object oriented

platform independent

Support multithreading

Safety

6. What is bytecode? Benefits of bytecode?

Bytecode: A unit of measurement for data in a computer

Benefits: Bytecode solves the problem that java programs can run on different systems without repeated compilation

7. What is java main class? What is the difference between the main class of an application and an applet?

java main class: the entry point for the program to run, there can only be one main class, including the main method

Difference: the main class in the applet must inherit JApplet or Applet class, must be public, and the application program is not fixed; the main class is the entry point of the java program

basic grammar

type of data

1. What is the basic data type?

Integer types: byte(1), short(2), int(4), long(8)

Floating point types: float (4), double (8)

Character type: char(2)

Boolean: boolean(1)

2. What is a reference data type?

kind

interface

array

coding

1. The encoding method adopted by java? Features?

Using the Unicode encoding standard

Make a unique value for each character, which can be used on any platform and language

note

1. Java annotations and their functions?

Single line comment: //

Multi-line comments: /* */

Documentation comments: /** */

Role: increase the readability of the code, which is conducive to modification and communication

2. What is the difference between access modifiers?

public: all classes, interfaces, variables, and methods are visible

protected: classes in the same package, and subclasses are visible, and classes cannot be modified

default: Visible in the same package, no modifiers are used

private: Only the current class is visible

keywords

1.final role

Modified classes cannot be inherited

Decorated methods cannot be overridden

Modified variables cannot be changed

2. What is the difference between final, finally, and finalize?

final: can modify classes, methods, variables

finally: Acts in the try-catch code block, regardless of whether an exception occurs, it must be executed

finalize: The garbage collector in jvm calls the finalize method to recycle garbage

3. How to use this?

a pointer to itself;

  1. direct reference, pointing to the object itself

  1. Formal parameters and member names have the same name, use this to distinguish

  1. Refer to the constructor of this class (put in the first statement of the constructor)

4. How to use super?

a pointer to the parent class (nearest);

  1. Direct reference, pointing to the object of the current parent class

  1. When the member variable or method in the subclass has the same name as the member variable or method in the parent class, it is used to distinguish

  1. Call the constructor of the parent class (should be placed on the first line)

5. What is the difference between this and super?

  1. Essentially, this is a pointer, but super is a keyword

  1. this refers to its own objects, variables, and constructors, and super refers to objects, variables, methods, and constructors in the parent class

6. What is the application and principle of static?

It will be executed once when the class is loaded, and space will be allocated during loading. Most of the time, some initialization operations that only need to be performed once will be placed in it;

Modified variables or methods are independent and shared by all instances;

  1. Modified member variables

  1. Decorate member methods

  1. static block

  1. Modified class (inner class)

  1. Static import package

7. What is the difference between identifiers and keywords

When writing a program, you need to name a class, object, method, etc., and you have an identifier; an identifier is a name

Keywords are identifiers that are given special meaning

8. Increment and decrement operators

Add/subtract when the sign is before, and add/subtract when the sign is after

flow control statement

1. What are the differences and functions of break, continue, and return?

break: Jump out of the loop and execute the following code

continue: Jump out of this loop and continue to execute the next loop

return: Jump out of the loop, do not execute the following code, return directly

2. How to break out of multiple nested loops?

Use break to jump out of the loop and continue to execute the following code

object oriented

1. What is object-oriented?

Object-oriented (behavior-oriented): Abstract real transactions into objects, and then complete the assembly, with encapsulation, inheritance, polymorphism, low performance, and easy maintenance.

2. What are the three major characteristics of object-oriented?

Encapsulation: privatize the properties of an object while providing methods for external access

Inheritance: Create new classes based on existing classes, and new classes can add new functions. It is easy to reuse previous code through inheritance

Polymorphism: The reference variable defined by the parent class or interface can point to the instance object of the subclass or the specific implementation class, which improves the scalability of the program

3. What is the difference between an abstract class and an interface?

  1. Abstract classes can have method implementations; interfaces do not have methods and constructors

  1. Abstract classes are inherited by extends; interfaces are implemented by implement

  1. The definition keywords are different, the interface uses interface, and the abstract class abstract

  1. To access attributes, the interface uses public by default; the abstract method can be arbitrary;

  1. An abstract class can inherit a class or implement multiple interfaces; an interface can inherit one or more other interfaces

  1. The new method of abstract class does not need to change the implementation class; the new method of interface must change the implementation class;

  1. Static code blocks can be used in abstract classes, but not in interfaces

4. What is a construction method?

The function is to complete the initialization of the class object. If there is no construction method, there will be a default construction method without parameters;

characteristic:

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

  1. There is no return value, but nothing can be used void

  1. Automatically, no need to call

5. What is the difference between member variables and local variables?

  1. Member variables are valid for the entire class; local variables are generally valid within methods or statements

  1. Member variables exist with the creation of the object and disappear with the disappearance of the object; local variables are automatically released when the method is used up or the statement ends

  1. Member variables have default initial values; local variables do not, and must be assigned before use

6. What is an inner class? Types of inner classes

Put the definition of a class inside another class; the inner class itself is an attribute, defined in the same way as other attributes.

Member inner class: defined inside the class, non-static; can access all methods and variables outside; usage: outer class instance.new inner class ();

Local inner class: the inner class defined in the method; can access all methods and variables outside; if defined in a static method, only the static methods and variables of the outer class can be accessed; usage: in the corresponding method, inside new kind();

Anonymous inner class: an inner class without a name; must inherit an abstract class or implement an interface; cannot define static; parameters are declared final; cannot be abstract; usage: new class {}

Static inner class: static class defined inside the class; all static variables of the outer class can be accessed; usage: new outer class. static inner class ();

7. Advantages and usage scenarios of internal classes

advantage:

  1. Invisible to other classes, local encapsulation

  1. Implemented multiple inheritance

  1. Easy to define callbacks

  1. Can access the contents of the object of the outer class that created it, including private data

scenes to be used:

  1. Make the code more flexible and scalable;

  1. Cannot be accessed by other classes except external classes

  1. resolve some non-object-oriented statement blocks

8. The difference between rewriting and overloading

  1. Overloading is the same method name, different parameters; rewriting is subclass rewriting parent class method;

  1. Overloads are defined within a class; overrides are defined between parent and child classes

  1. Overloading has no requirements for return; overriding requires a return type, and there are compatible return types

  1. The number, type, and order of overloaded parameters can be different; the rewriting parent and child classes must be the same

  1. Overloading has no requirements on access modifiers; overridden access modifiers must be greater than the parent class

9. The difference between == and equals

==: The basic data type is compared to the value, and the reference data type is compared to the memory address

equals:

No overrides: equivalent to ==

Rewrite: compare whether the content is the same

10.hashCode与equals

hashCode: determine the position of the object in the hash table, and return an int; when adding an object, first calculate the hashcode of the object to determine the joining position. If the hashCode already exists, call equals to check whether the objects are the same.

If two objects are the same, the hashCode must be the same; if the hashCode is the same, the objects are not necessarily the same.

11. Object equality and reference equality

Object equality: refers to whether the content is equal; reference equality: refers to whether the memory addresses are equal.

12. What is pass by value

When calling the method, the actual parameters are responsible for copying and passing them to the method. Even if the parameters are modified, the actual parameters will not be affected

13. The difference between passing by value and passing by reference

Value transfer: When the method is called, the passed parameters are directly copied, and are not related to each other after the transfer;

Passing by reference: When the method is called, the parameter passed is the reference address, which points to the same reference address before and after the transfer;

14. The role of the instanceof keyword

Binary operator, which detects whether an object is a class

15.java automatic unboxing and boxing

Unboxing: converting the wrapper type into a basic data type

Boxing: converting basic data types into wrapper types

16. The difference between deep copy and shallow copy

Shallow copy: For the basic data type, the value is passed directly and stored in another space. Modifying the value will not affect the copied value; for the reference data type, the address is passed without opening up a new space, so the two values ​​will point to the same The memory address, after modification, will affect the value of the copy source.

Deep copy: A new allocation of memory will copy all attributes and copy the memory pointed to by the attributes, which is slow and expensive

17. The difference between super and extends in generics

In generics, extends is the upper limit of the set type wildcard; super is the lower limit of the set type wildcard

18. What are the java class loaders?

1. Bootstrap class loader (startup class loader)

2. Extend the class loader

3. System class loader

4. Custom loader

19. Parental delegation principle

A class loader does not load itself immediately after receiving the request, but first checks whether the parent class loader has been loaded, iterates layer by layer, and when the top-level loader is gone, it will delegate to load the specified class

20. The difference between String, StringBuffer and StringBuilder

String objects are immutable, so they can be understood as constants and thread-safe;

StringBuffer and StringBuilder are variable (append method)

StringBuffer adds a synchronization lock, so it is thread-safe; StringBuilder does not add a lock, so the thread is not safe;

When String is changed, a new object will be generated; StringBuffer and StringBuilder will modify themselves

Summarize:

Operate a small amount of data with String

Use StringBuilder to operate a large amount of data under a single thread

Use StringBuffer to operate a large amount of data under multi-line

reflection

1. What is reflection?

Can dynamically obtain information and call object methods; know all methods and attributes for any class and any method;

2. The way of reflection

  1. Realized by new object

  1. Achieved by path

  1. Implemented by class name

3. Advantages and disadvantages of reflection

Advantages: can dynamically load classes, improve code flexibility

Disadvantages: resource consumption, performance is the bottleneck

4. Reflection application scenarios

  1. Dynamic Configuration Properties

  1. get an instance of a class

  1. Load xml or configuration files into memory

abnormal

1. Abnormal classification

Error: error

Exception: exception

  1. runtime exception

  1. compile time exception

2. Exception handling

  1. Throw an exception: throw

  1. Declaring exceptions: throws

  1. Catching exceptions: try-catch-finally

3. The difference between throw and throws

  1. throw is used inside the method and can only be used to throw one exception; throws is used in the method declaration and can throw multiple exceptions;

  1. Throws means that an exception may occur; throw will definitely throw some kind of exception object

4. The difference between Error and Exception

Error: Error, the compiler does not detect or catch it, once it occurs, the program usually terminates

Exception: Abnormal, which can be caught and processed in the program. After the processing is successful, it will run normally

5. What is the difference between runtime exception and general exception

Runtime exceptions: exceptions that occur during runtime

General exceptions: all exceptions except runtime exceptions

the difference:

If it is mandatory to handle, use a general exception, otherwise use a runtime exception

6. Which part of try-catch-finally can be omitted

catch can be omitted

Because try handles runtime exceptions, try+catch handles runtime exceptions + common exceptions, so when handling runtime exceptions, catch can be omitted

7. What are the common runtime exceptions?

  1. class conversion exception

  1. Array out of bounds

  1. null pointer

  1. Data storage exception

other

  1. What steps does the browser go through from sending a request to receiving a response

2. What is a cross-domain request? What is the problem? how to solve this

Cross-domain request: When a browser initiates a network request, it will check whether the corresponding protocol, domain name, and port are consistent with the current web page, and if they are inconsistent, restrict them

Problem: When the network request is inconsistent with the current web page, the browser will restrict it for user safety

solve:

Use the form of jsonp (the underlying script can be cross-domain)

Background control, first access the interface in the same domain name, and then use tools such as httpClient to call the target interface

Use a gateway and hand it over to background services for cross-domain access

response add header

3. What is front-end separation

Separation of front and back ends is to separate data operation and display. The front end focuses on data display, and displays the data visually through text, pictures or icons. The backend focuses on data manipulation. The front end sends the data to the back end, and the back end modifies the data.

The backend provides an interface for the frontend to call to trigger the operation of the backend on the data

4. Why is String designed to be immutable?

String is modified by final and becomes a constant, which cannot be modified. Attaching a new value also recreates a string;

benefit:

  1. String constant pool is used

  1. Cache hashCode

  1. Key used as hashCode

  1. thread safe (with immutability)

5. TCP's three-way handshake and four-way wave

The tcp protocol is a transport layer protocol in the 7-layer network protocol, which is responsible for the reliable transmission of data; when establishing a tcp connection, it needs to be established through a three-way handshake. The process is:

1. The client sends a SYN to the server

2. After the server receives the SYN, it sends a SYN_ACK to the client

3. After the client receives the SYN_ACK, it sends an ACK to the server

When the tcp connection is disconnected, the port needs to be waved four times. The process is:

1. The client sends FIN to the server

2. After the server receives the FIN, it sends an ACK to the client

3. After the server has processed all the data, it sends a FIN to the client, indicating that the server can disconnect

4.客户端接收服务端的FIN,向服务端发送ACK,表示客户端可以断开连接

Guess you like

Origin blog.csdn.net/qq_35056891/article/details/129558777