Java high-frequency basic interview questions——(3)

王林
Release: 2020-08-31 16:49:48
forward
2417 people have browsed it

Java high-frequency basic interview questions——(3)

The interview questions are as follows:

1. What is the use of a.hashCode()? What does it have to do with a.equals(b)?

(Recommendations for more related interview questions:java interview questions and answers)

The hashCode() method corresponds to the hash value of the object's integer type. It is commonly used in hash-based collection classes, such as Hashtable, HashMap, LinkedHashMap, etc. It is particularly closely related to the equals() method. According to the Java specification, two objects that are equal using the equal() method must have the same hash code.

2. The difference between byte stream and character stream

To output a piece of binary data to a device one by one, or to read a piece of binary data one by one from a device, no matter What are input and output devices? We need to use a unified way to complete these operations and describe them in an abstract way. This abstract description method is named IO stream. The corresponding abstract classes are OutputStream and InputStream. Different implementation classes are Represents different input and output devices, which all operate on bytes.

Everything in the computer ultimately exists in binary byte form. For frequently used Chinese characters, first get the corresponding bytes, and then write the bytes to the output stream. When reading, the first thing read is bytes, but if we want to display it as characters, we need to convert the bytes into characters. Because such needs are widespread, Java specifically provides a character stream wrapper class.

The underlying device always only accepts byte data. Sometimes if you want to write a string to the underlying device, you need to convert the string into bytes before writing. The character stream is a wrapper for the byte stream. The character stream directly accepts strings. It converts the strings into bytes internally and then writes them to the underlying device. This provides a little bit of information for us to write or read strings to the IO device. convenient.

When converting characters to bytes, pay attention to encoding issues, because converting a string into a byte array is actually converting it into a certain encoded byte form of the character, and the opposite is true when reading.

3. What is java serialization and how to implement java serialization? Or please explain the role of Serializable interface.

We sometimes transfer a java object into a byte stream or restore it from a byte stream into a java object. For example, we need to store the java object on the hard disk or transmit it to the network. For other computers, in this process we can write our own code to convert a java object into a byte stream in a certain format and then transmit it.

However, jre itself provides this support. We can call the writeObject method of OutputStream to do it. If we want java to do it for us, the object to be transmitted must implement the serializable interface. In this way, when javac compiles Special processing will be performed so that the compiled class can be operated by the writeObject method. This is called serialization. The class that needs to be serialized must implement the Serializable interface. This interface is a mini interface in which there is no need to implement methods. Implements Serializable is just to mark that the object can be serialized.

For example, in web development, if the object is saved in the Session, tomcat needs to serialize the Session object to the hard disk when restarting, and this object must implement the Serializable interface. If an object is to be transmitted over the network through a distributed system, the object being transmitted must implement the Serializable interface.

4. Describe the principle mechanism of JVM loading class files?

The loading of classes in JVM is implemented by ClassLoader and its subclasses. Java ClassLoader is an important Java runtime System Components. It is responsible for finding and loading classes from class files at runtime.

(Recommended related tutorials:java introductory tutorial)

5. What is the difference between heap and stack.

Java's memory is divided into two categories, one is stack memory and the other is heap memory. Stack memory means that when the program enters a method, a private storage space will be allocated for this method to store local variables inside this method. When this method ends, the stack allocated to this method will be released. The variables will also be released accordingly.

The heap is a memory that has different functions from the stack. It is generally used to store data that is not in the current method stack. For example, objects created using new are placed in the heap, so it will not end with the method. And disappear. After the local variables in the method are modified with final, they are placed in the heap instead of the stack.

6. What is GC? Why is there GC?

GC means garbage collection (Gabage Collection). Memory processing is where programmers are prone to problems, such as forgotten or wrong memory. Recycling can cause instability or even collapse of the program or system. The GC function provided by Java can automatically detect whether the object exceeds the scope to achieve the purpose of automatically recycling memory. The Java language does not provide an explicit operation method to release allocated memory.

7. Advantages and principles of garbage collection. And consider 2 recycling mechanisms.

A notable feature of the Java language is the introduction of a garbage collection mechanism, which solves the most troublesome memory management problem for C programmers. It makes Java programmers no longer need to consider memory management when writing programs. Due to the garbage collection mechanism, objects in Java no longer have the concept of "scope". Only references to objects have "scope".

Garbage collection can effectively prevent memory leaks and effectively use available memory. The garbage collector usually runs as a separate low-level thread, clearing and recycling objects in the memory heap that have died or have not been used for a long time under unpredictable circumstances. Programmers cannot call the garbage collector in real time to deal with certain objects. objects or all objects are garbage collected.

The recycling mechanism includes generational copy garbage collection, marked garbage collection, and incremental garbage collection.

8. What is the basic principle of the garbage collector? Can the garbage collector reclaim memory immediately? Is there any way to actively notify the virtual machine to perform garbage collection?

For GC, when the programmer creates an object, GC begins to monitor the address, size and usage of the object. Usually, GC uses a directed graph to record and manage all objects in the heap. In this way it is determined which objects are "reachable" and which objects are "unreachable". When the GC determines that some objects are "unreachable", the GC is responsible for reclaiming these memory spaces.

Programmers can manually execute System.gc() to notify the GC to run, but the Java language specification does not guarantee that the GC will be executed.

9. In Java, what is the difference between throw and throws?

throw is used to throw an instantiated object of the java.lang.Throwable class, which means that you can throw through the keyword throw Throw an Exception, such as:
throw new IllegalArgumentException(“XXXXXXXXX″)

The function of throws is as part of the method declaration and signature, and the method is thrown the corresponding exception so that the caller can handle it. In Java, any unhandled checked exception is forced to be declared in the throws clause.

(Recommended video tutorial:java course)

10. Will there be a memory leak in java? Please describe it briefly.

First explain what a memory leak is: The so-called memory leak means that an object or variable that is no longer used by the program has been occupied in memory. There is a garbage collection mechanism in Java, which can ensure that when the object is no longer referenced, the object will be automatically cleared from the memory by the garbage collector.

Because Java uses a directed graph for garbage collection management, it can eliminate the problem of reference cycles. For example, if there are two objects that reference each other, as long as they are not reachable from the root process, the GC can also recycle them. .

Memory leaks in Java: Memory leaks are likely to occur when long-lived objects hold references to short-lived objects. Although short-lived objects are no longer needed, because of the long The life cycle object holds its reference and cannot be recycled. This is the scenario where memory leaks occur in Java. In layman's terms, the programmer may create an object and never use it again, but the object is always being used. Reference, that is, the object is useless but cannot be recycled by the garbage collector. This is the situation where memory leaks may occur in Java. For example, in the cache system, we load an object and place it in the cache (for example, in a global map object). ), and then never use it again. This object is always referenced by the cache, but is no longer used.

The above is the detailed content of Java high-frequency basic 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!