Several questions about Java

Source: Internet
Author: User
Document directory
  • 1. Differences between heap and stack in Java
  • 2. Differences between sleep and wait in Java
  • 3. the Java design pattern you have heard

I have interviewed many companies recently. I am deeply touched by some questions about the test and interview. Although I am familiar with Java, I am a little confused about some details. The following is a simple example:

1. Differences between heap and stack in Java

In fact, this problem is not very difficult. However, many programmers may ignore this point, especially when garbage collection. You may be used to Java's GC mechanism. In fact, if you can understand GC carefully, we can improve the efficiency of the execution program. Stack and stack are the most basic things.
Both heap and stack Are Java used to store data in the memory.
To put it simply, the new object is used, that is, the dynamic memory allocation during runtime, which exists in the heap. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated. The Java Garbage Collector automatically collects the data that is no longer used.
To improve program efficiency, when we don't need to use this object, we just need to simply stop referencing it, that is, assign this object to NULL, instead of waiting for the garbage collection bin to slowly recycle it.
Some basic data types and references of Java generally exist in the stack. The stack can be accessed quickly and data in the stack can be shared.

2. Differences between sleep and wait in Java

If you have never used a thread or rarely used a thread, this question is really hard to answer and confusing.
First, both are methods to temporarily stop the thread execution. But they are quite different.
The most basic point is that sleep is the method in the Thread, and wait is the method in the Object.
Sleep is used to temporarily sleep the current thread and can be placed anywhere. Wait allows the current thread to temporarily discard the right to use the object for waiting, and must be placed in the synchronous method or synchronization block. When sleep is used, the thread does not give up the right to use the object, that is, it does not release the object lock. Therefore, when a synchronization method or synchronization block is used to access sleep, other threads cannot be accessed. The wait will release the object lock, that is, the current thread gives up the right to use the object, so that other threads can access it. When the thread executes the wait method, another thread needs to call notify to wake up. Sleep only temporarily sleeps for a certain period of time. After the time is reached, it will automatically resume running without the need for other threads to wake up.
In short, sleep is just a temporary sleep. Wait is usually used to synchronize with threads and is often used with Y and yyall.

3. the Java design pattern you have heard

If you haven't written the code for many years, this question may be really hard to answer. Maybe many beginners will say that MVC in Java Web development is not counted? In fact, there are not many design patterns I have heard of before. The most likely is the Factory mode and Singleton mode. I recommend Java Design Patterns for designing Patterns, which will benefit you a lot.
Additional http://www.jdon.com/designpatterns/
Http://www4.it168.com/jtzt/shenlan/tech/javadesignpatern/index.html
Http://www.ibm.com/developerworks/cn/java/design/
Http://www.cn-java.com/www1? Action-category-catid-101
The above four websites are all about Java design patterns, and you can learn a lot.

Source:Http://www.blogjava.net/landon/archive/2010/04/16/318476.html

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.