Collection of classic Java interview questions (3)

王林
Release: 2020-07-07 16:54:26
forward
2222 people have browsed it

Collection of classic Java interview questions (3)

1. The difference between String, StringBuffer and StringBuilder

(Related tutorial recommendations: java interview questions)

The value of String is immutable, which results in a new String object being generated every time an operation is performed on String.

Objects of the StringBuffer and StringBuilder classes can be modified multiple times without generating new unused objects

The speed is: StringBuilder > StringBuffer > String

The biggest difference between the StringBuilder class and StringBuffer is that StringBuilder's methods are not thread-safe.

Look at the picture:

Collection of classic Java interview questions (3)

(Recommended tutorial: java entry program)

2. All All classes inherit from the object class. What are the direct subclasses of the object class that you have used? What are the commonly used methods of the object class?

  • Boolean

  • Character

  • Class

  • ClassLoader

  • Compiler

  • Enum

  • String

  • System

  • Thread

Common methods of Object class

  • toString ();

  • ## equals();

  • hashCode();

3. What are generics, how are they used, and what are their benefits?

is a special type that defers the work of type clarification until when the object is created or the method is called.

Parameterized type, pass the type as a parameter. It is a design pattern that supports polymorphism very well, such as various containers.

Benefits:

Advances runtime problems to the compilation period, avoiding forced type conversion.

4. Why should serialversionUID be used for Java object serialization?

1. Why serialize objects?

  • The process of converting objects into byte sequences is called object serialization

  • The process of restoring a byte sequence into an object is called object deserialization

2. There are two main uses for object serialization:

(1) Permanently save the object's byte sequence to the hard disk, usually in a file;

(2) Transmit the object's byte sequence on the network.

3. Why use SerialversionUID

If the user does not declare a serialVersionUID by himself, the interface will generate a serialVersionUID by default. If a new field is added to the object, the serialversionUID allocated by the system will change, resulting in Deserialization exception.

(Video tutorial recommendation:

java video tutorial)

5. Advantages and disadvantages of reflection

1. Advantages

Reflection improves the flexibility and scalability of Java programs, reduces coupling, and improves adaptive capabilities. It allows programs to create and control objects of any class without raising the hard-coded target class.

Reflection is not available in some other commonly used languages, such as C, C, Fortran or Pascal.

Java reflection technology has a wide range of applications, such as software testing, JavaBean, etc.

Many popular open source frameworks such as Struts, Hibernate, and Spring use this technology in their implementation.

2. Disadvantages

(1) Performance Overhead

Reflection includes some dynamic types, so the JVM cannot optimize these codes. Therefore, reflective operations are much less efficient than those non-reflective operations. We should avoid using reflection in frequently executed code or in programs with high performance requirements.

(2) Security Restrictions

Using reflection technology requires that the program must run in an environment without security restrictions. This is a problem if a program must run in a security-restricted environment, such as an applet. .

(3) Exposure of Internals

Since reflection allows code to perform some operations that are not allowed under normal circumstances (such as accessing private properties and methods), using reflection may cause leading to unexpected side effects. The code has functional errors that reduce portability. Reflective code breaks abstraction, so when the platform changes, the behavior of the code may also change.

The above is the detailed content of Collection of classic Java interview questions (3). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template