search
HomeJavaJavaInterview questionsCollection of classic Java interview questions (3)

Collection of classic Java interview questions (3)

Jul 07, 2020 pm 04:54 PM
javaInterview questions

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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.