In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the practical interview questions for Java development?" in the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. What is the difference between processes, threads, and co-programs?
In short, a process is the basic unit of program running and resource allocation. A program has at least one process and a process has at least one thread. The process has independent memory units during execution, while multiple threads share memory resources, which reduces the number of switches and is more efficient. A thread is an entity of a process, the basic unit of cpu scheduling and dispatch, and a smaller basic unit that can run independently than a program. Multiple threads in the same process can execute concurrently.
2. Do you know anything about daemon threads? What is the difference between it and a non-daemon thread
When the program is finished, jvm waits for the non-daemon thread to complete and shuts down, but jvm does not wait for the daemon thread. The most typical example of a daemon thread is the GC thread.
3. What is multithreaded context switching
Multi-threaded context switching refers to the process of switching CPU control from one running thread to another thread that is ready and waiting for CPU execution rights.
4. How to create two threads? What's the difference between them?
By implementing java.lang.Runnable or by extending the java.lang.Thread class. Implementing the Runnable interface may be better than extending Thread. There are two reasons:
Java does not support multiple inheritance. So extending the Thread class means that this subclass cannot extend other classes. A class that implements the Runnable interface may also extend another class.
Classes may only need to be executable, so inheriting the entire Thread class is too expensive.
5. What's the difference between the start () and run () methods in the Thread class?
The start () method is used to start the newly created thread, and the run () method is called internally by start (), which is different from calling the run () method directly. When you call the run () method, it will only be called in the original thread, and no new thread will start the start () method will start the new thread.
6. How do you detect whether a thread holds an object monitor?
The Thread class provides a holdsLock (Object obj) method that returns true if and only if the monitor of the object obj is held by a thread. Note that this is a static method, which means that "some thread" refers to the current thread.
7. What is the difference between Runnable and Callable?
The return value of the run () method in the Runnable interface is void, and what it does is simply to execute the code in the run () method; the call () method in the Callable interface has a return value, which is a generic type, and can be used with Future and FutureTask to obtain the result of asynchronous execution.
This is actually a very useful feature, because one of the important reasons why multithreading is more difficult and more complex than single threading is that multithreading is full of unknowns, does a thread execute? How long has a thread been executing? Has the expected data been assigned when a thread executes? There is no way to know that all we can do is wait for the completion of this multithreaded task. On the other hand, Callable+Future/FutureTask can easily obtain the results of multi-thread running, and can cancel the task of the thread if the waiting time is too long to get the needed data.
What garbage collection algorithms do you know?
Garbage collection is very easy to understand in theory, and there are several specific methods:
1. Mark-clear
two。 Mark-copy
3. Marking-finishing
4. Generation by generation recovery
8. How to judge whether an object should be reclaimed
This is the so-called object viability judgment, there are two commonly used methods: 1. Citation counting method; 2. Object accessibility analysis. Because the reference counting method has the problem that GC can not be carried out due to mutual reference, object reachability analysis algorithms are often used in JVM virtual machines at present.
10. Explain garbage collection briefly.
The most basic method of Java garbage collection mechanism is generation-by-generation recycling. The area in memory is divided into different generations, and the object is stored in the region of the corresponding generation according to its survival time. The general implementation is divided into three generations: young, old and permanent. Memory allocation occurs in the younger generation. When an object lives long enough, it is copied to the older generation. Different garbage collection algorithms can be used for different generations. The starting point of generation division is the statistical law obtained after the study of the survival time of objects in application. Generally speaking, most objects in an application have a short survival time. For example, the survival time of local variables is only during the execution of the method. Based on this, garbage collection algorithms for the younger generation can be very targeted.
What happens when you call System.gc ()?
Notify GC to start working, but the time when GC actually starts is uncertain.
What is the length of int in 12-bit and 64-bit JVM?
In Java, the length of an int type variable is a fixed value, independent of the platform, and is 32 bits. This means that the length of the int type is the same in 32-bit and 64-bit Java virtual machines.
12. The difference between int and Integer
Integer is the packaging type of int, and the two are automatically converted in unpacking and packing. Int is a primitive type that stores values directly, while integer is an object that points to it with a reference.
14. Who uses more memory, int or Integer?
Integer objects take up more memory. Integer is an object that needs to store the object's metadata. But int is a primitive type of data, so it takes up less space.
15. The difference between String, StringBuffer and StringBuilder
String is a string constant, final modifier: StringBuffer string variable (thread safe)
StringBuilder string variable (thread unsafe).
16, String and StringBuffer
The main difference between String and StringBuffer is performance: String is an immutable object, and each operation on a String type is equivalent to producing a new String object and then pointing to a new String object. So try not to do a lot of splicing operations on String, otherwise it will produce a lot of temporary objects, which will cause GC to start working and affect system performance.
StringBuffer operates on the object itself, not on generating new objects, so we recommend using StringBuffer when there is a large number of splices.
However, it should be noted that now JVM will optimize String stitching:
String s = "This is only" + "simple" + "test" will be directly optimized by the virtual machine to String s = "This is only simple test", and there is no splicing process.
17, StringBuffer and StringBuilder
StringBuffer is a thread-safe variable string, and its internal implementation is a variable array. StringBuilder is new to jdk 1.5 and is similar to StringBuffer, but not thread-safe. Therefore, using StringBuilder can achieve better performance without multithreading problems.
What is a compiler constant? What are the risks of using it?
Public static immutable (public static final) variables are what we call compile-time constants, where public is optional. In fact, these variables are replaced at compile time because the compiler knows the values of these variables and that they cannot be changed at run time. One problem with this approach is that you use a public compile-time constant in an internal or third-party library, but this value is later changed by someone else, but your client is still using the old value, and you have even deployed a new jar. To avoid this, be sure to recompile your program when you update dependent JAR files.
19. What type is used in java to indicate that the price is better?
Use BigDecimal if you are not particularly concerned about memory and performance, otherwise use double types with predefined precision.
20. How to convert byte to String
You can use the constructor that String receives the byte [] parameter for the transformation, with the correct encoding to be used, or the platform default encoding, which may or may not be the same as the original one.
21. Can I strongly convert int to byte? What's the problem?
We can do a cast, but in Java, int is 32-bit and byte is 8-bit, so the high 24 bits of int type will be discarded if cast, and the range of byte type is from-128 to 128.
This is the end of the content of "what are the practical interview questions for Java development?" thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.