Java face test-Basic Article Five __java

Source: Internet
Author: User
Tags garbage collection serialization wrapper
Java face test-Basic Article Five

41, A.hashcode () what is the use. What is the relationship to A.equals (b).
The Hashcode () method corresponds to the hash value of the integral type of the object. It is commonly 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 the byte stream and the character streams

To output a piece of binary data to a device, or read a piece of binary data from one device at a time, regardless of what input is, we're going to do it in a uniform way, in an abstract way of describing it, this abstract description is named IO stream, The corresponding abstract classes are OutputStream and inputstream, and different implementation classes represent different input and output devices, all of which operate on a byte.

Everything in the computer is ultimately a binary byte form. For frequently used Chinese characters, you first have to get their corresponding bytes, and then write the bytes to the output stream. Read the first byte, but we want to display it as a character, we need to convert bytes to characters. Because of such a wide range of requirements, Java specifically provides a character stream wrapper class.

The underlying device will always accept byte data, sometimes to write strings to the underlying device, and to convert the string to bytes before writing. The character stream is the wrapper of the byte streams, which is the direct acceptance of the string, which internally converts the strings to bytes and then to the underlying device, which provides a little convenience for us to write to or read the string to the IO device.

When converting characters to bytes, you should pay attention to the problem of encoding, because the string is converted into a byte array, which is actually a coded byte form that turns into that character.


43, what is Java serialization, how to implement Java serialization. Or, explain the role of the serializable interface.

We sometimes turn a Java object into a byte stream or revert from a byte stream to a Java object, for example, to store a Java object to a hard disk or to another computer on the network. This process allows us to write our own code to transform a Java object into a byte stream in a format.

However, the JRE itself provides this support, we can call the OutputStream WriteObject method, if we want Java to help us, the object to be transferred must implement the Serializable interface, so that the Javac compile-time will be special processing , 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 no implementation method is required, implements serializable just to annotate that the object is serializable.

For example, in web development, if an object is saved in session, Tomcat serializes the session object to the hard disk during reboot, and the object must implement the Serializable interface. If an object is to be transmitted through a distributed system, the object being transmitted must implement the Serializable interface.

44. Describe the principle mechanism of the JVM loading class file?

The loading of classes in the JVM is implemented by ClassLoader and its subclasses, and the Java ClassLoader is an important Java Run-time system component. It is responsible for finding and loading classes of class files at run time.

45, heap and stack what is the difference.

Java memory is divided into two classes, one is stack memory, the other is heap memory. Stack memory is when a program enters a method that allocates a private storage space for this method to store local variables within the method, and when the method ends, the stack assigned to the method is released, and the variables in the stack are released.

A heap is a memory that differs from the stack and is typically used to hold 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 placed in the heap instead of on the stack after the final modification is used.

46, what is GC? Why should there be a GC?

GC is the meaning of garbage collection (Gabage Collection), memory processing is where programmers are prone to problems, forgetting or incorrect memory recycling can cause the program or system to be unstable or even crash, Java provides a GC feature that automatically monitors whether an object is out of scope to automatically reclaim memory, and the Java language does not provide a display operation to free allocated memory.

47, the advantages and principles of garbage collection. and consider 2 kinds of recycling mechanisms.

A notable feature of the Java language is the introduction of a garbage collection mechanism that solves the problem of the most troublesome memory management problems of C + + programmers, making it unnecessary for Java programmers to consider memory management when writing programs. Because of 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 memory that can be used. The garbage collector is usually run as a separate low-level thread, unpredictable in the case of the dead in the heap or long time unused objects for scavenging and recycling, programmers can not call the garbage collector in real time to garbage collection of an object or all objects.

The recycle mechanism has generational replication garbage collection and tag garbage collection, incremental garbage collection.

48. What is the basic principle of the garbage collector? Can the garbage collector reclaim memory immediately? What is the way to actively notify the virtual machine for garbage collection.

For GC, when a programmer creates an object, the GC begins to monitor the address, size, and usage of the object. Generally, the GC uses a heap to record and manage all the objects in the heap (). In this way, determine which objects are "accessible" and which objects are "unreachable." When the GC determines that some objects are "unreachable," the GC is responsible for reclaiming the memory space.

The programmer can manually execute System.GC () to notify the GC to run, but the Java language specification does not guarantee that the GC will execute.


49, Java, throw and throws what is the difference between

Throw is used to throw an instantiated object of the Java.lang.Throwable class, meaning that you can throw a exception through the keyword throw, such as:
throw new IllegalArgumentException ("Xxxxxxxxx″")

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


Is there a memory leak in the 50,java, please describe it briefly.

First explain what a memory leak is: A memory leak means that an object or variable that is no longer being used by a program is always in memory. There is a garbage collection mechanism in Java that guarantees that objects will automatically be purged from memory by the garbage collector when they are no longer referenced.

Because Java uses a graph-like approach to garbage collection management, you can eliminate the problem of reference loops, such as having two objects and referencing each other, so that GC can recycle them as long as they are not up to the root process.

Memory leaks in Java: A long lifecycle object holds a reference to a short lifecycle object and is likely to have a memory leak, although the short lifecycle object is no longer needed, but because the long lifecycle object holds its reference and cannot be reclaimed, this is the scenario where memory leaks occur in Java, and in layman's sense, Is that the programmer may have created an object, this object is not used in the future, the object has been referenced, that the object is useless but can not be reclaimed by the garbage collector, this is the case of memory leaks in Java, for example, the caching system, we loaded an object in the cache ( For example, in a global map object, and then never use it, the object has been cached for reference, but is no longer used.


Java Companion public number of major companies to organize a number of commonly used interview pen questions, for everyone in the spare time to learn some of the topics, accumulated over time, wait for the interview, everything is ripe, the interview will naturally be easier.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.