In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the basic interview questions of Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the basic interview questions of Java"?
41. 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. It is often used in hash-based collection classes, such as Hashtable, HashMap, LinkedHashMap, and so on. It is particularly closely related to the equals () method. According to the Java specification, two objects that use the equal () method to determine equality must have the same hash code.
42. 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 the input and output device is, we need to complete these operations in a unified way and describe them in an abstract way, which is called IO stream, and the corresponding abstract classes are OutputStream and InputStream. Different implementation classes represent different input and output devices. They all operate on bytes.
Everything in the computer ultimately exists in the form of binary bytes. For frequently used Chinese characters, first get the corresponding bytes, and then write the bytes to the output stream. When reading, the first thing to read is a byte, but to display it as a character, we need to convert the byte to a character. Because of the wide range of such requirements, Java specifically provides character stream wrapper classes.
The underlying device always accepts only byte data, and sometimes to write a string to the underlying device, you need to convert the string into bytes before writing. The character stream is the wrapper of the byte stream, while the character stream accepts the string directly, which internally converts the string into bytes and then writes it to the underlying device, which provides a little convenience for us to write or read strings to the IO device.
When converting characters to bytes, you should pay attention to the problem of coding, because when a string is converted into a byte array, it is actually converted into a byte form of some encoding of the character, and reading is also vice versa.
What is java serialization and how to implement java serialization? Or please explain what the Serializable interface does.
We sometimes send a java object into a byte stream or recover it from a byte stream to a java object. For example, to store a java object on a hard disk or transfer it to another computer on the network, we can write our own code to change a java object into a byte stream of a certain format and then transfer it.
However, jre itself provides this support, and we can call the writeObject method of OutputStream to do it. If we want java to do it for us, the object to be transferred must implement the serializable interface, so that javac compiles with special processing and the compiled class can be manipulated by the writeObject method, which is called serialization. The class that needs to be serialized must implement the Serializable interface, which is a mini interface, where there is no need to implement methods, implements Serializable is simply to mark that the object is serializable.
For example, in web development, if an object is saved in Session and tomcat serializes the Session object to the hard disk on restart, the object must implement the Serializable interface. If the object is to be transmitted through the network through the distributed system, the transmitted object must implement the Serializable interface.
44. Describe the principle and mechanism of loading class files by JVM?
Class loading in JVM is implemented by ClassLoader and its subclasses, and Java ClassLoader is an important component of the Java runtime system. It is responsible for finding and loading classes of class files at run time.
45. What's the difference between heap and stack?
Java memory is divided into two types, stack memory and heap memory. Stack memory means that when a program enters a method, it allocates a separate private storage space for the method to store local variables inside the method. When the method ends, the stack assigned to the method is freed, and the variables in the stack are also released.
A heap is memory that is different from the stack and is generally used to store data that is not in the current method stack. For example, objects created with new are placed in the heap, so it does not disappear with the end of the method. The local variables in the method are decorated with final and placed in the heap instead of the stack.
46. What is GC? Why is there a GC?
GC means garbage collection (Gabage Collection). Memory processing is a place where programmers are prone to problems. Forgetting or incorrect memory collection will lead to instability or even collapse of the program or system. The GC function provided by Java can automatically monitor whether the object exceeds the scope so as to achieve the purpose of automatic memory collection. Java language does not provide a display operation method to release allocated memory.
47. The advantages and principles of garbage collection. Two kinds of recovery mechanisms are considered.
A remarkable feature of Java language is the introduction of garbage collection mechanism, which solves the memory management problem that C++ programmers have the most headache. 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 object references have "scope".
Garbage collection can effectively prevent memory leakage and effectively use the available memory. The garbage collector usually runs as a separate low-level thread to unpredictably clear and collect dead or unused objects in the memory heap. Programmers cannot call the garbage collector to garbage collect an object or all objects in real time.
The recycling mechanism includes generation-by-generation replication garbage collection and marked garbage collection, 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 for garbage collection?
For GC, when a programmer creates an object, GC begins to monitor the address, size, and usage of the object. In general, GC records and manages all objects in the heap (heap) in a directed graph. In this way, determine which objects are "reachable" and which are "unreachable." When GC determines that some objects are "unreachable", GC has the responsibility to reclaim this memory space.
Programmers can manually execute System.gc () to tell GC to run, but the Java language specification does not guarantee that GC will be executed.
49. What's the difference between throw and throws in Java
Throw is used to throw an instantiated object of the java.lang.Throwable class, meaning that you can throw an Exception through the keyword throw, such as:
Throw new IllegalArgumentException ("XXXXXXXXX")
The role of throws is as part of the method declaration and signature, and the method is thrown with 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.
Will there be a memory leak in 50century java? please briefly describe it.
First explain what a memory leak is: a 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 ensures that when the object is no longer referenced, the object will be automatically cleared from memory by the garbage collector.
Because Java uses directed graph for garbage collection management, it can eliminate the problem of reference loop, for example, there are two objects that reference each other, as long as they and the root process are unreachable, then GC can also recycle them.
Memory leaks in java: memory leaks are likely to occur when long-lifecycle objects hold references to short-lifecycle objects. Although short-lifecycle objects are no longer needed, they cannot be recycled because long-lifecycle objects hold its references. This is the scenario where memory leaks occur in java. Generally speaking, programmers may create an object and never use it again. This object has always been referenced, that is, this object is useless but cannot be reclaimed by the garbage collector, which is a situation where memory leaks can occur in java. For example, in a 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 has been referenced by the cache but is no longer used.
At this point, I believe you have a deeper understanding of "what are the basic interview questions of Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.