search
HomeJavaJavaInterview questionsBasic Java interview questions (4)
Basic Java interview questions (4)Dec 04, 2019 pm 03:04 PM
java

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
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

归纳整理JAVA装饰器模式(实例详解)归纳整理JAVA装饰器模式(实例详解)May 05, 2022 pm 06:48 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor