国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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

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

Aug 31, 2020 pm 04:49 PM
java Interview questions

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!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

Understanding the Java Virtual Machine (JVM) Internals Understanding the Java Virtual Machine (JVM) Internals Aug 01, 2025 am 06:31 AM

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

How does garbage collection work in Java? How does garbage collection work in Java? Aug 02, 2025 pm 01:55 PM

Java's garbage collection (GC) is a mechanism that automatically manages memory, which reduces the risk of memory leakage by reclaiming unreachable objects. 1.GC judges the accessibility of the object from the root object (such as stack variables, active threads, static fields, etc.), and unreachable objects are marked as garbage. 2. Based on the mark-clearing algorithm, mark all reachable objects and clear unmarked objects. 3. Adopt a generational collection strategy: the new generation (Eden, S0, S1) frequently executes MinorGC; the elderly performs less but takes longer to perform MajorGC; Metaspace stores class metadata. 4. JVM provides a variety of GC devices: SerialGC is suitable for small applications; ParallelGC improves throughput; CMS reduces

Understanding Network Ports and Firewalls Understanding Network Ports and Firewalls Aug 01, 2025 am 06:40 AM

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

go by example defer statement explained go by example defer statement explained Aug 02, 2025 am 06:26 AM

defer is used to perform specified operations before the function returns, such as cleaning resources; parameters are evaluated immediately when defer, and the functions are executed in the order of last-in-first-out (LIFO); 1. Multiple defers are executed in reverse order of declarations; 2. Commonly used for secure cleaning such as file closing; 3. The named return value can be modified; 4. It will be executed even if panic occurs, suitable for recovery; 5. Avoid abuse of defer in loops to prevent resource leakage; correct use can improve code security and readability.

Comparing Java Build Tools: Maven vs. Gradle Comparing Java Build Tools: Maven vs. Gradle Aug 03, 2025 pm 01:36 PM

Gradleisthebetterchoiceformostnewprojectsduetoitssuperiorflexibility,performance,andmoderntoolingsupport.1.Gradle’sGroovy/KotlinDSLismoreconciseandexpressivethanMaven’sverboseXML.2.GradleoutperformsMaveninbuildspeedwithincrementalcompilation,buildcac

See all articles