gogoWebsite

Java Junior and Middle School Interview Questions

Updated to 18 days ago

Java Junior and Middle School Interview Questions
1. Please tell me the difference between scope public, private, protected, and not writing
Answer: The visible ranges of these four scopes are shown in the following table.
Note: If no access modifier is written on the modified element, it means friendly.
Scope Current class Same package (package) Sub-class other packages (package)
public √ √ √ √
protected √ √ √ ×
friendly √ √ × ×
private √ × × ×
2. Interface and abstract classes
An interface can be said to be a special case of an abstract class, and all methods in an interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to the public static final (I need to say here that since a variable is modified by final, then this variable is a constant!!! The variable must be initialized into a constant!!!)
Let’s compare the grammatical differences between the two:
1. Abstract classes can have construction methods, but not in interfaces.
2. There can be ordinary member variables in abstract classes, but there are no ordinary member variables in the interface!!! (Note that the focus is ordinary, that is, non-static and variables!!!)
3. Abstract classes can contain non-abstract ordinary methods. All methods in the interface must be abstract, and there cannot be non-abstract ordinary methods.
4. The access types of abstract methods in abstract classes can be public, protected and (the default type, although there is no error in eclipse, it should not work), but the abstract methods in the interface can only be of public type, and the default is the public abstract type.
three. The difference between String, StringBuffer and StringBuilder
String string constant
StringBuffer String Variable (Thread Safe)
StringBuilder String Variable (non-thread safe)
Four. Is there a length() method for arrays? Is there a length() method for String?
Answer: The array does not have the length() method, it has the length attribute. String has the method length().
five. Multithreaded part
1.What is multithreading?
In one application, there are multiple different execution paths at the same time.
2. Let’s talk about the benefits of multi-threading?
Provides program efficiency.
3. What is the difference between threads and processes?
A thread is an execution path of a process, and a process is a collection of threads.
4. What is thread synchronization and asynchronous?
Thread synchronization means that the next thread will execute after the current thread is executed.
Thread asynchronously means that in an application, there are multiple different execution paths at the same time. For example javaweb ajax android handler
5. How to synchronize between threads
Synchronized, wait and notify between threads
6. What is thread insecure? How to solve it? (Focus)
It means that sharing the same data on multiple threads will be disturbed by other threads. How to solve it: Use thread synchronization technology and use synchronized. Let one thread execute, and let another thread execute.
7. How to create a thread? How many ways are there?
Inherit the thread class, override the run method, implement the Runnalbe interface, rerun the run method, and start a thread with start();
8. Is it better to use the Runnalbe interface? Or is it better to inherit the Thread class?
It is good to implement the Runnalbe interface, because the implemented interface can continue to be inherited. If you inherit the Thread class, you cannot inherit it.
What is the difference between () and wait()?
a. sleep is to allow the current thread to specify the sleep time and then continue to work without releasing the lock
b. Let the current thread wait wait until the thread notify() wakes it up. Release the lock
six. Collection part
What's the difference from Set and Map?
Collections in Java include three major categories: Set, List and Map. They are all in packages. Set, List and Map are interfaces and they have their own implementation classes. The main implementation classes of Set include HashSet and TreeSet, the main implementation classes of List include ArrayList, and the main implementation classes of Map include HashMap and TreeMap.
Objects in Set are not sorted in a specific way, and there are no duplicate objects. But some of its implementation classes can sort objects in a collection in a specific way, such as the TreeSet class, which can be sorted by default or can be customized through implementation interfaces.
Objects in List are sorted by index position, and there can be repeated objects, allowing objects to be retrieved according to the index position of the object in the collection, such as obtaining elements in the List collection through (i).
Each element in the Map contains a key object and a value object that appears in pairs. The key object cannot be repeated, the value object can be repeated.
What are the characteristics of the three interfaces, Map, and Set when accessing elements?
list:Storage: Ordered Repeatable
Access: For loop, foreach loop, iterator iterator iterator.
set:Storage: Unordered Non-repeat
Access: Foreach loop, iterator iterator
map: Storage: It stores a one-to-one pair mapping "key=value", the key value is unordered and does not repeat. Value value can be repeated
Access: You can convert the key value in the map into set storage, then iterate over this set, and use (key) to get the value
It can also be converted to an entry object and iterate with an iterator
3. Say the storage performance and characteristics of ArrayList, Vector, LinkedList
ArrayList and Vector both use arrays to store data. The number of elements in this array is larger than the actual stored data to add and insert elements. They both allow indexing elements directly by sequence number. However, inserting elements involves memory operations such as array element movement, so indexing data is fast and inserting data is slow. Vector uses synchronized method (thread safety), and performance is usually worse than ArrayList. LinkedList uses a bidirectional linked list to implement storage. Indexing data by sequence number requires forward or backward traversal, but when inserting data, only the front and back items of this item need to be recorded, so the insertion speed is faster.
and hashtable
The contains method of HashTable was removed, but the containsValue() and containsKey() methods were added.
Synchronous, while HashMap is asynchronous, the efficiency forces hashTable to be higher.
A null key value is allowed, while a hashTable does not.


seven. Let’s talk about the Java reflection mechanism and common scenarios.
The JAVA reflection mechanism is that in the running state, for any class, you can know all the properties and methods of this class; for any object, you can call any methods and properties of it; this function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language.
Application of reflection mechanisms, such as spring framework, reverse code, such as decompilation

eight. Frame part
1. Talk about Spring?
Spring is a lightweight control inversion (IoC) and section-oriented (AOP) container framework.
IOC (Inversion of Control) control inversion: Write class creation and dependencies in spring configuration file, and dynamically inject the configuration file when the program is running, so that light coupling is achieved.
AOP (Aspect Oriented Programming) Oriented Programming: Extracts the functions of relatively independent program logic for public services such as security, transactions, and logs, and uses spring configuration files to dynamically insert these functions when the program runs, allowing programmers to focus more on the implementation of business logic, realizes programming according to aspects, and improves maintainability and reusability.
2. Talking about the principles of SpringIOC and SpringAOP?
SpringIOC, control inversion is a programming idea, while dependency injection is control inversion in spring in concrete implementation. Using factory mode, you only need to configure the corresponding beans in the spring configuration file and set relevant properties, so that the spring container can generate class instance objects and manage objects. When the spring container is started, spring will initialize all the beans you configured in the configuration file, and then when you need to call it, assign the beans that have been initialized to the class that you need to call these beans (assuming that the class name is A). The allocation method is to call A's setter method to inject, without you needing new these beans in A. The factories mainly involve patterns and reflection mechanisms.
SpringAOP oriented programming, or AOP, is a programming technology that allows programs to modularly cut horizontally concerns, or cross-cut typical division of responsibilities such as logging and transaction management. SpringAop is a dynamic proxy using Java. It mainly uses Java's proxy mode and reflection mechanism.

What is the underlying implementation mechanism?
Using Demo4j (parsing XML) + Java reflection mechanism
Demo4j is actually parsing XML. Instantiate beans using reflection mechanism.

What are the ways to inject?
Set Injection
Constructor injection
Method injection of static factory
Method injection of instance factory
What annotations does Spring have?
@Autowired(injected by type)
@Service (labeled as injection as service layer)
@@Transactional Things Annotation
@Controller(identifies the controller bean id)
@RequestMapping (represents the mapped URL path)
Briefly describe the advantages and disadvantages of Spring?
What are the advantages of Spring? ?
1. Reduce the coupling between components and realize the decoupling between the various layers of the software.
2. You can use a variety of services that are easy to provide, such as transaction management, message services, etc.
3. Container provides singleton mode support
4. Containers provide AOP technology, which can easily be used to implement functions such as permission interception, running monitoring and other functions.
5. Containers provide a number of auxiliary classes, which can speed up application development
Provides integration support for mainstream application frameworks, such as hibernate, JPA, Struts, etc.
It is a low intrusive design, with extremely low code pollution
8. Independent of various application servers
The DI mechanism reduces the complexity of business object replacement
The high openness of the application does not force the application to rely entirely on Spring. Developers can freely choose part or all of spring.
shortcoming:
A large number of reflection mechanisms were used. The reflection mechanism takes up a lot of memory.
Simple execution process

#{} and ${} are different, that can prevent SQL injection.
13 Let’s talk about things and the configuration of spring things.
9. What is a singleton? What are the ways to write a singleton?
Singleton category: lazy singleton, hungry singleton
The singleton mode has the following characteristics:
1. There can only be one instance in a singleton class.
2. The singleton class must create its own unique instance.
3. The singleton class must provide this instance to all other objects.
① Lazy single case

  1. // Lazy singleton class. Instantiate itself on the first call
  2. public class Singleton {
  3.  private Singleton() {}  
    
  4.  private static Singleton single=null;  
    
  5. //Static factory method
  6.  public static Singleton getInstance () {  
    
  7.       if (single == null) {    
    
  8.           single = new Singleton();  
    
  9.       }    
    
  10.         return single;  
    
  11.     }  
    
  12. }  
    

② Single case of hungry man
13. //Hungry singleton class. When the class is initialized, it has been instantiated by itself.
14. public class Singleton1 {
15. private Singleton1() {}
16. private static final Singleton1 single = new Singleton1();
17. //Static factory method
18. public static Singleton1 getInstance() {
19. return single;
20. }
21. }
What is the difference between lazy and hungry?
In terms of name, hungry and lazy people,
Once the class is loaded, the singleton is initialized to ensure that the singleton already exists when getInstance.
Lazy guys are relatively lazy. They only go back to initialize this singleton when they call getInstance.
In addition, the following two methods are distinguished from the following two points:

1. Thread safety:
The Hungry Man style is born thread-safe and can be used directly for multi-threading without problems.
Lazy style is non-thread-safe. There are several ways to write thread safety, namely 1, 2, and 3 above. These three implementations have some differences in resource loading and performance.
2. Resource loading and performance:
The Hungry Style instantiates a static object while the class is created. Regardless of whether this singleton will be used later, it will occupy a certain amount of memory. However, accordingly, the speed will be faster when the first call is called, because its resources have been initialized.
As the name suggests, lazy man style will delay loading, and the object will be instantiated when the singleton is used for the first time. Initialization must be done on the first call. If there is more work to do, there will be some delay in performance, and then it will be the same as the Hungry Man style.
ten. Database interview questions
1. Basic SQL add, delete, modify and check, left and right connection, inner connection, order by, group by
2. Index.
3. Stored procedures.
4. Trigger.
5. Lock: Shared lock, exclusive lock
engine.
eleven. Js
Basic parameters
type: ‘GET’, // This is the request method. It can be GET or POST method. The default is GET.
url: ’ xxx’, // This is the requested connection address. Generally, this address is a connection to the front end from the background, and you can just write it directly.
dataType: ‘json’, // This is the data type returned by the background. Generally, it is a json data, and it will be OK after traversing the front end.
async: true, // Default is true. When true, all requests are asynchronous requests. If you need to send a synchronous request, it must be set to false,
data: {
// Parameters to be passed
‘xxx’ : ‘xxx’, },
2. How to solve cross-domain problems
Jsonp, disadvantage, can only get requests.
The following is a brief understanding
twelve. Difference between cookies and sessions
The difference between cookies and sessions:
1. The cookie data is stored on the customer's browser and the session data is placed on the server.
2. Cookies are not very safe. Others can analyze the local COOKIE and perform COOKIE fraud.
Session should be used considering security.
3. The session will be saved on the server within a certain period of time. When the number of accesses increases, it will take up more performance on your server
Considering the reduction of server performance, COOKIE should be used.
4. The data saved by a single cookie cannot exceed 4K. Many browsers restrict a site to save up to 20 cookies.
5. So personal suggestions:
Store important information such as login information as SESSION
Other information can be placed in COOKIE if it needs to be retained.

Thirteen. What is a distributed system.
fourteen. Let’s talk about the single sign-on system (sso).