Home  >  Article  >  Java  >  Basic Java interview questions (4)

Basic Java interview questions (4)

(*-*)浩
(*-*)浩Original
2019-12-04 15:04:052569browse

Basic Java interview questions (4)

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

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.

The difference between byte stream and character stream (Recommended learning: java interview questions)

To output a piece of binary data one by one to a certain device, or read a piece of binary data one by one from a device, no matter what the input and output device is, we must use a unified way to complete these operations.

Describe in an abstract way. This abstract description method is named IO stream. The corresponding abstract classes are OutputStream and InputStream. Different implementation classes represent different input and output devices. They are Operates 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.

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 transfer it to For other computers on the network, 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.

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.

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 a different function 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 on the heap, so they will not disappear when the method ends. After the local variables in the method are modified with final, they are placed in the heap instead of the stack.

What is GC? Why is there GC?

GC means garbage collection (Gabage Collection). Memory processing is where programmers are prone to problems. Forget Or incorrect memory recycling can lead to instability or even crash 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 reclaiming memory. The Java language does not provide a display operation method to release allocated memory.

The 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. It clears and recycles objects in the memory heap that have died or have not been used for a long time under unpredictable circumstances. Programmers cannot do this in real time. Call the garbage collector to garbage collect an object or all objects.

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

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, determine 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.

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 pass The keyword throw throws an Exception, such as:

throw new IllegalArgumentException(“XXXXXXXXX″)

The role of throws is as part of the method declaration and signature. 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.

Is there a memory leak in java? Please describe 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 the 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 the short-lived objects are no longer needed, because of the long-lived objects Holding a reference to it and preventing it from being recycled is the scenario where memory leaks occur in Java.

In layman's terms, the programmer may create an object and never use the object again. However, the object is always referenced. That is, the object is useless but cannot be recycled by the garbage collector. This is Memory leaks may occur in java.

For example, in the cache system, we load an object and put 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. use.

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

Statement:
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