Java Foundation Interview (v)

Source: Internet
Author: User

41. What is the use of A.hashcode ()? What is the relationship with A.equals (b)?
The Hashcode () method corresponds to the hash value of the integer type of the object. It is often used for 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 character stream and the byte stream

To output a piece of binary data to a device one at a time, or to read a piece of binary data from one device at a time, no matter what the input is, we need to do it in a uniform way, describing it in an abstract way, which 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 bytes.

Everything in the computer ends up being binary bytes. For frequently used Chinese characters, the corresponding byte is first obtained, and then the bytes are written to the output stream. Read, the first read is the byte, but we want to display it as a character, we need to convert the bytes into characters. Because of this wide range of requirements, Java specifically provides a character stream wrapper class.

The underlying device always accepts only byte data, and sometimes writes a string to the underlying device, which needs to be converted to bytes and then written. The character stream is the wrapper of the byte stream, and the character stream is directly accepting the string, which internally turns the strings into bytes and writes to the underlying device, which provides a little bit of convenience for us to write or read the string to the IO device.

When converting a character to a byte, pay attention to the problem of encoding, because the string into a byte array, in fact, is converted to the character of some kind of encoded byte form, reading is the other way around.

43. What is Java serialization and how do I implement Java serialization? Or, explain the role of the serializable interface.

There are times when we turn a Java object into a byte stream or revert from a byte stream to a Java object, for example, to store Java objects on a hard disk or to send to other computers on the network. In this process, we can write our own code to transform a Java object into a stream of bytes in a format.

However, the JRE itself provides this support, we can call OutputStream's WriteObject method to do, if we want to let Java help us do, the object to be transferred must implement the Serializable interface, so that 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 there is no need to implement a method, implements serializable just to annotate that the object is serializable.

For example, in web development, if the object is stored in the session and Tomcat is restarting to serialize the session object to the hard disk, the object must implement the Serializable interface. If an object is to be transmitted over a distributed system, the transmitted object 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 Java ClassLoader is an important Java Runtime system component. It is responsible for locating and loading classes of class files at run time.

45. 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 is when a program enters a method, it allocates a private storage space for the method, which stores the local variables inside the method, and when the method ends, the stack allocated to the method is freed, and the variables in the stack are freed.

A heap is a memory that is different 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 the stack, using the final decoration.

46. What is GC? Why do you have a GC?

GC is the meaning of garbage collection (Gabage Collection), memory processing is where programmers are prone to problems, forgetting or wrong memory recycling can cause program or system instability or even crashes, The GC functionality provided by Java can automatically monitor whether an object exceeds the scope to achieve the purpose of automatically reclaiming memory, and the Java language does not provide a way to release the displayed operation of the 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, which makes it possible for the C + + programmer to solve the most troublesome memory management problems, which makes it unnecessary for Java programmers to consider memory management when writing programs. Because of the garbage collection mechanism, objects in Java no longer have a "scope" concept, and only references to objects have a 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, and in unpredictable cases the dead or unused objects in the heap are purged and reclaimed, and the programmer cannot call the garbage collector in real time to garbage collection of an object or all objects.

The recycling mechanism has generational replication garbage collection and token garbage collection, incremental garbage collection.

48. What is the basic principle of the garbage collector? Can the garbage collector reclaim memory right away? Is there any way to proactively notify a virtual machine for garbage collection?

For GC, when a programmer creates an object, the GC starts to monitor the object's address, size, and usage. Typically, a GC uses a graph to record and manage all objects in the heap. In this way, you determine which objects are "accessible" and which objects are "unreachable." When the GC determines that some objects are unreachable, it is the responsibility of the GC to reclaim those memory spaces.

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. What is 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 a exception through the keyword throw, such as:
throw new IllegalArgumentException ("Xxxxxxxxx″")

The function of throws is as part of the method declaration and signature, and the method is thrown with the appropriate 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 the 50,java, please describe it briefly.

Explain what a memory leak is: the so-called memory leak refers to an object or variable that is not being used by the program has been occupied in memory. There is a garbage collection mechanism in Java that ensures that objects are automatically erased from memory by the garbage collector when the object is 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 reclaim them as long as they and the root process are unreachable.

Memory leaks in Java: Long life-cycle objects that hold references to short life-cycle objects are likely to have a memory leak, even though short life-cycle objects are no longer needed, but because long-life-cycle objects hold their references and cannot be recycled, this is where memory leaks occur in Java, in layman's words, Is that the programmer might have created an object that would never be used in the future, and this object has been referenced, that is, the object is useless but cannot be reclaimed by the garbage collector, which is a possible memory leak in Java, for example, the cache system, we loaded an object in the cache ( For example, in a global map object), and then no longer using it, the object has been cached for reference, but is no longer used.

Transfer from http://www.cnblogs.com/peke/p/7858455.html

Java Foundation Interview (v)

Related Article

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.