Common Java face Test _java

Source: Internet
Author: User
Tags arrays static class thread class stringbuffer

This article mainly for everyone to organize Java common face test questions for your reference, the specific contents are as follows

1. The difference between sleep and wait in Java

① the two methods come from different classes, sleep comes from the thread class, and wait comes from the object class.
Sleep is the static class method of thread, who calls who goes to bed, even if the B's Sleep method is called in a thread, actually or a goes to bed, to let B-thread sleep to call in B's code.

② Lock: The most important is that the sleep method does not release the lock, and the wait method frees the lock so that other threads can use the synchronized control block or method.

Sleep does not transfer system resources, wait is into the thread waiting for the pool waiting, transfer system resources, other threads can occupy the CPU. The general wait does not increase the time limit, because if the waiting thread does not run enough resources to come out, it will be no use, until the other thread calls the Notify/notifyall wakeup wait pool all the threads, before entering the ready queue waiting for the OS to allocate system resources. Sleep (milliseconds) can be specified in time to wake it up automatically, if the time is not only called interrupt () forced interrupt.

The role of Thread.Sleep (0) is to "trigger the operating system to immediately reboot the CPU competition".

③ use: Wait,notify and notifyall can only be used in synchronous control methods or in synchronized control blocks, while sleep can be used anywhere.

 Synchronized (x) { 
  x.notify () 
  /or Wait () 
 }

2. The difference between HashMap and Hashtable in Java

① Historical reasons: Hashtable is given to the old Dictonary class, HashMap is a implementation of the map interface introduced by Java1.2

②HASHMAP allows null key-value pairs, while Hashtable does not allow

③hashtable synchronization, while HashMap is not synchronized, more efficient than Hashtable

3. Please describe the difference between throw and throws in the midst of an anomaly.

①throw represents an action, an action that throws an exception, and a throws represents a state in which a method may have an exception thrown
②throw is used in method implementations, while throws is used in method declarations
③throw can only be used to throw an exception, and throws may throw multiple exceptions

4. The difference between memory overflow and memory leak

Memory overflow out of memory, which means that the program does not have enough memory space for it to use when applying for memory, and it appears out of the memory; For example, an integer is applied, but the number that has long to save is the memory overflow.

Memory leak memory leak, refers to the program in the application of memory, can not release the requested memory space, a memory leakage hazard can be ignored, but the memory leak accumulation consequences are very serious, no matter how much memory, sooner or later will be taken up by the light.

Memory leak will eventually lead out of the memory!

Memory overflow is that you ask to allocate more memory than the system can give you, the system does not meet the requirements, resulting in overflow.

A memory leak is when you apply to the system to allocate memory for use (new), but after the use is not returned (delete), the result of the piece of memory you apply to you can no longer access (perhaps you have lost its address), and the system can not assign it to the required program. A dish can only hold 4 fruits in every way, and you have 5, so you can't eat it on the ground. This is overflow! For example, stack, stack when you do into the stack must produce space overflow, called overflow, stack empty when the stack also produces space overflow, called underflow. Is that the allocated memory is not enough to drop the sequence of data items, called a memory overflow.

In the manner in which they occur, memory leaks can be categorized into 4 categories:

① frequent memory leaks. The code that has a memory leak is executed multiple times, causing a memory leak each time it is executed.
② accidental memory leaks. The code that occurs with a memory leak only occurs under certain circumstances or procedures. The frequent and incidental are relative. For a given environment, the occasional may become a frequent occurrence. So test environments and test methods are critical to detecting memory leaks.
③ a one-time memory leak. The code that has a memory leak is executed only once, or because of an algorithm flaw, there will always be a single and one memory leak. For example, allocating memory in the constructor of a class does not release that memory in the destructor, so a memory leak occurs only once.
④ an implicit memory leak. The program keeps allocating memory while it is running, but it does not release memory until the end. Strictly speaking, there is no memory leak, because the final program frees up all of the requested memory. But for a server program, it can take days, weeks, or months to run out of memory, which may result in the eventual exhaustion of all of the system's memory. So, we call this kind of memory leak as an implicit memory leak.

From the user's point of view of the program, the memory leak itself will not produce any harm, as a general user, do not feel the existence of memory leaks. What really harms is the accumulation of memory leaks, which ultimately consumes all the memory of the system. In this sense, a one-time memory leak is harmless because it does not accumulate, and the implicit memory leak is very harmful because it is more difficult to detect than frequent and accidental memory leaks.

5. The difference between String,stringbuffer and StringBuilder

① variable and not variable
The String class uses a character array to hold the strings, as follows, because there is a "final" modifier, so you know that the string object is immutable.

   Private final char value[];

Both StringBuilder and StringBuffer inherit from the Abstractstringbuilder class, and in Abstractstringbuilder the string is also saved using a character array, as follows: Both objects are mutable.

    Char[] value;

② is multithreaded security

Objects in string are immutable and can be understood as constants, apparently thread-safe.

Abstractstringbuilder is the common parent class of StringBuilder and StringBuffer, which defines the basic operations of strings, such as expandcapacity, append, inserts, IndexOf, and other public methods.

StringBuffer is thread-safe by adding a synchronous lock to a method or by synchronizing the method that is invoked. Look at the following source code:

Public synchronized StringBuffer reverse () { 
  super.reverse (); 
  return this; 
 } 
 
 public int indexOf (String str) {return 
  indexOf (str, 0);  There is a public synchronized int indexOf (String str, int fromindex) method 
 

StringBuilder does not have a synchronized lock on the method, so it is not thread safe.

③stringbuilder and StringBuffer in common

StringBuilder and StringBuffer have a common parent class Abstractstringbuilder (abstract Class).

One of the differences between an abstract class and an interface is that a common method of subclasses can be defined in an abstract class, and subclasses simply need to add new functionality and do not have to repeat the existing methods, while the interface simply defines the method's declarations and constants.

StringBuilder, StringBuffer methods call public methods in Abstractstringbuilder, such as Super.append (...). Just stringbuffer will add synchronized keyword on the method and synchronize.

Finally, if the program is not multithreaded, then using StringBuilder is more efficient than stringbuffer.

6. The difference between an array and a linked list

Both belong to a data structure

In terms of the logical structure:
The ① array must define the fixed length (number of elements) beforehand, and it cannot adapt to the dynamic increase or decrease of the data. When the data is increased, it may exceed the number of elements originally defined, resulting in a waste of memory when data is reduced, and arrays can be accessed directly from the subscript.
② linked list Dynamic storage allocation, can adapt to the dynamic increase and decrease of data, and can easily insert, delete data items. (when inserting, deleting items in an array, you need to move other data items, very cumbersome) the list must find the next element based on the next pointer.

From memory storage:
The ① (static) array allocates space from the stack, which is convenient and fast for programmers, but has little freedom.
The ② chain list allocates space from the heap, the freedom is big but the application management is more troublesome.
As you can see from the above comparison, if you need fast access to data, you should use arrays if you have little or no elements to insert and delete, instead, you need to use a linked list data structure if you need to insert and delete elements frequently.

7. The difference between ArrayList and LinkedList

①arraylist is to implement the data structure based on dynamic array, linkedlist the data structure based on the linked list.
② is better than LinkedList for random access get and set,arraylist because LinkedList moves the pointer.
③ is more advantageous for add and remove operations and remove,linedlist because ArrayList to move the data.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.