1. Can I access non-static variables in the static environment?
The static variable belongs to the class in Java, and it has the same value in all instances. Static variables are initialized when the class is airborne into Java virtual. If your code tries to access a non-static variable without an instance, the compiler will give an error because the variables have not been created yet and are not associated with any instances.
2. What is automatic unboxing?
Auto-Boxing is a conversion of the Java compiler between the base data type and the corresponding object wrapper type. For example: Convert int to integer,double into double, and so on. The reverse is automatic unpacking.
3. What is the difference between an interface and an abstract class?
Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, and the difference is that:
- All methods in an interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.
- A class can implement many interfaces, but only one abstract class is inherited
- Class if you want to implement an interface, it must implement all the methods that the interface declares. However, a class can not implement all methods of an abstract class declaration, and of course, in this case, the class must also be declared abstract.
- An abstract class can implement an interface without providing an interface method implementation.
- The variables declared in the Java interface are final by default. An abstract class can contain non-final variables.
- The member functions in the Java interface are public by default. The member functions of an abstract class can be private,protected or public.
- An interface is absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be called if it contains the main method.
4. Java so-called reference passing:
In fact, it is also a value of the transfer. There is no real reference passing in Java
5. How do I create threads in several different ways? Which one do you like? Why?
There are three ways to create threads:
- Inherit the thread class
- Implementing the Runnable Interface
- Applications can use the executor framework to create a thread pool
Implementing the Runnable interface is more popular because this does not require inheriting the thread class. This requires multiple inheritance (while Java does not support multiple inheritance) in cases where other objects have been inherited in the design of the application, and only interfaces can be implemented. At the same time, the thread pool is very efficient and easy to implement and use.
6. How does thread synchronization work inside the monitor? What level of synchronization should the program do?
Monitors and locks are used in Java virtual machines. The monitor monitors a block of synchronization code to ensure that only one thread executes the synchronized code block at a time. Each monitor is associated with an object reference. The thread does not allow synchronization code to be executed until the lock is acquired.
7. How do I ensure that n threads can access n resources without causing a deadlock?
When using multithreading, a very simple way to avoid deadlocks is to specify the order in which locks are acquired and force threads to acquire locks in the order specified. Therefore, if all the threads lock and release the lock in the same order, there will be no deadlock.
What is the difference between 8.Iterator and listiterator?
Their differences are listed below:
- The iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.
- Iterator to a collection can only be forward traversal, listiterator can be either forward or back.
- Listiterator implements the iterator interface and includes other functions such as adding elements, replacing elements, getting the index of the previous and subsequent elements, and so on.
A summary of some of the more easily mistaken concepts in the JAVA learning process