2021/1/13 Niuke hundred questions summary

I do not produce code, I am just a code porter

1. Pass by value and pass by reference

Code:

public class SendValue{
    
    
	public String str="6";
	public static void main(String[] args) {
    
    
		SendValue sv=new SendValue();
		sv.change(sv.str);
		System.out.println(sv.str);
	}
	public void change(String str) {
    
    
		str="10";
	}
}

The result of the above code is: 6

In Java, all values ​​are passed. In this question, we must first understand the memory allocation mechanism.

public String str="6"

The memory allocation mechanism of this sentence is:
First, create a new reference variable str to an object of the String class in the stack.

Then look for an address with a value of "6" in the stack

If there is, it is assigned to the newly created str

If not, a new String object "6" is created in the heap, named newObj, and a new space is created in the stack, named newStack, newStack is used to store the address of newObj, and finally, str points to newStack.

In the course of this question :

First, pass the address of the str parameter to the change function

However, since Java is passed by value, it is equivalent to creating a new k object on the stack. The address of this k object is the same as the address of str.

Then, the k="10" statement starts to execute, looking for an address with no "10" in the stack, so create a new one and return the address to k

At this time, in fact, k and str have no relationship, because str points to "6" and k points to "10", and the result is of course the same.
Insert picture description here

2. The number of objects generated by String splicing

String test="javaandpython"; 
String str1="java"; 
String str2="and"; 
String str3="python"; 
System. out. println(test=="java"+"and"+"python"): 
System. out. println(test ==str1 + str2 + str3);

For the above code, the result is true false

This is because the string literal splicing operation is executed during the compilation of the Java compiler, that is, the three literals "java", "and" and "python" are directly performed "+" operation to get a "javaandpython" Constant, and put this constant directly into the string pool.

The "+" operation of string reference is executed during Java runtime, that is, str1 + str2 + str3 will be calculated during program execution, and it will recreate a spliced ​​string object in the heap memory.

And there will be str1, str2 and str3 in the string constant pool. How many new objects are created here is related to whether there is str1\str2\str3 in the original string constant pool. If it exists before, no new objects will be created.

In summary, the literal "+" splicing is performed during compilation, and the spliced ​​string is stored in the string pool; and the "+" splicing operation of the string reference is actually performed at runtime, the newly created character The string is stored in a pile.

3. Some knowledge of Hibernate

1. What is lazy loading?

definition:

Lazy load is the default loading method of Hibernate3 association objects. The lazy load mechanism is proposed to avoid some unnecessary performance overhead.

That is, only when the data is really needed, the data loading operation is actually performed. Lazy loading is a measure used in hibernate to improve query efficiency. Its opposite is immediate loading.

2. How to implement lazy loading?

Hibernate 2 implements lazy loading in two ways:

  • Entity object
  • set

Hibernate 3 introduced a new loading method: lazy loading of properties

Generally use the load() method to implement lazy loading:

When the load method is called to load the object, the *** object is returned, and the sql statement is issued when the content of the object is actually used

3. Other introductions to Hibernate

Hibernate uses Java reflection mechanism instead of bytecode enhancement program to achieve transparency

The performance of Hibernate is very good because it is a lightweight framework. The flexibility of mapping is excellent. It supports various relational databases, from one-to-one to many-to-many complex relationships.

4. Optimize the 7 measures encouraged by Hibernate

  1. Try to use many-to-one and avoid single-to-many
  2. Flexible use of one-to-many
  3. Instead of one-to-one, use many-to-one instead of one-to-one
  4. Configure the object cache, do not use the collection cache
  5. One-to-many use Bag, many-to-one use Set
  6. Inheritance use show polymorphism HQL: from object polymorphism = "exlicit" to avoid investigating all objects
  7. Eliminate large tables and use secondary cache

4. Some methods in the Object class

Insert picture description here

5.Synchronized and volatile features

Synchronized guarantees three major properties: atomicity, orderliness, and visibility

Volatile guarantee: orderliness, visibility

  1. Order rearrangement prohibited
  2. Ensures the visibility of different threads operating on this variable, that is, if a thread modifies a variable value, the new value is immediately visible to other threads
  3. Does not guarantee atomicity (unsafe threads)

Comparison of synchronized keyword and volatile keyword :

  1. The volatile keyword is a lightweight implementation of thread synchronization, so the volatile performance is definitely better than the synchronized keyword.
  2. But the volatile keyword can only be used for variables and the synchronized keyword can modify methods and code blocks.
  3. The synchronized keyword has been implemented after JavaSE1.6, mainly including the introduction of biased locks and lightweight locks and other various optimizations in order to reduce the performance consumption caused by acquiring and releasing locks. The execution efficiency has been significantly improved, and the actual development is There are more scenarios for using the synchronized keyword.
  4. Multi-threaded access to the volatile keyword will not be blocked, while the synchronized keyword may be blocked
  5. The volatile keyword can guarantee the visibility of the data, but it cannot guarantee the atomicity of the data. The synchronized keyword can guarantee both.
  6. The volatile keyword is mainly used to solve the visibility of variables between multiple threads, while the synchronized keyword solves the synchronization of accessing resources between multiple threads.

6. Some considerations for abstract methods and abstract classes

  1. Abstract class cannot be instantiated , the work of instantiation should be done by its subclasses, it only needs to have a reference.
  2. Abstract methods must be overridden by subclasses.
  3. As long as the class contains an abstract method, the class must be defined as an abstract class, regardless of whether it contains other methods.
  4. The abstract class can contain concrete methods, of course, it does not need to contain abstract methods.
  5. Abstract cannot modify the same class in parallel with final.
  6. Abstract cannot modify the same method in parallel with private, static, final, or native.
  7. Abstract methods are not allowed to have method bodies and curly braces, and end with a semicolon .

7. The difference between HashMap and Hashtable

  1. Is thread safe: HashMap is not thread safe, HashTable is thread safe ; the internal methods of HashTable are basically modified by synchronized. (If you want to ensure thread safety, use ConcurrentHashMap!)

  2. Efficiency: Because of thread safety issues, HashMap is more efficient than HashTable. In addition, HashTable is basically
    eliminated, do not use it in the code;

  3. Support for Null key and Null value: In HashMap, null can be used as a key. There is only one such key, and
    there can be one or more keys whose corresponding value is null. However, as long as the key value put in the HashTable has a null value, a
    NullPointerException will be thrown directly .

  4. The difference between the initial size and the size of each expansion: If you do not specify the initial value of the capacity when you create it, the default initial size of Hashtable is 11, and each subsequent expansion, the capacity becomes the original 2n+1. The default initialization size of HashMap is 16. After each expansion, the capacity is doubled . If the initial value of the capacity is given when creating, then Hashtable will directly use the size you give, and HashMap will expand it to a power of 2 size.

  5. Underlying data structure: HashMap after JDK1.8 has a major change when resolving hash conflicts. When the length of the linked list is larger than the threshold (default is 8), the linked list is converted into a red tree to reduce search time . Hashtable has no such mechanism.

  6. The inherited parent class is different: Hashtable inherits from Dictionary class, while HashMap inherits from AbstractMap class . But both implement the Map interface.

8.try-catch-finally problem

  • Use try-catch to catch exceptions;
  • Use try-finally to clear the exception;
  • Use try-catch-finally to handle all exceptions. Choose one of the three

9.Statement, PreparedStatement, CallableStatement related knowledge

  1. Statement, PreparedStatement and CallableStatement are all interfaces (interface)

  2. Statement inherits from Wrapper , PreparedStatement inherits from Statement, CallableStatement inherits from PreparedStatement

  3. The Statement interface provides basic methods for executing statements and obtaining results;
    PreparedStatement interface adds methods for processing IN parameters;
    CallableStatement interface adds methods for processing OUT parameters.

  4. ①Statement: ordinary query SQL without parameters; supports batch update and batch deletion;
    ②PreparedStatement: variable parameter SQL, compiled once, executed multiple times, high efficiency; good security, effectively preventing Sql injection and other problems; support batch update , Batch delete;
    ③CallableStatement: inherited from PreparedStatement, supports SQL operations with parameters; supports calling stored procedures, and provides support for output and input/output parameters (INOUT);

  5. PreparedStatement is pre-compiled. There are several advantages to using PreparedStatement:
    ① When executing a variable parameter SQL, PreparedStatement is more efficient than Statement, because DBMS pre-compiling one SQL will certainly be more efficient than compiling one SQL multiple times.
    ②Good security, effectively preventing SQL injection and other problems.
    ③For statements that are repeatedly executed multiple times, the use of PreparedStament will be more efficient, and in this case it is also more suitable to use batch;
    ④The readability and maintainability of the code.

10. Four types and eight basic data types in Java:

  1. The first category: integer type byte short int long
  2. The second category: floating double
  3. The third category: logical boolean (it has only two values ​​that can be true and false)
  4. The fourth category: character type char

11. Two same two small one big principle

The method name is the same, the parameter type is the same
, the return type of the subclass is less than or equal to the return type of the parent method, the
exception thrown by the subclass is less than or equal to the exception thrown by the parent method,
and the access permission of the subclass is greater than or equal to the parent method access permission.

12.Java three major notes

  1. The three major annotations in Java are @Override @Deprecated @Suppresswarnings
  2. The @Override annotation table name subclass covers a certain method in the super class. If you write the wrong form of coverage, the compiler will report an error
  3. @Deprecated indicates that you do not want others to use this class, method, variable, etc. in the future
  4. @Suppresswarnings achieves the purpose of suppressing the compiler's warnings, but it is not recommended, because the later coders cannot understand the warnings prompted by the compiler and cannot better choose a better class to complete the task

13. The difference between abstract class and interface:

  1. Abstract classes can have constructors , but interfaces cannot have constructors.
  2. There can be ordinary member variables in abstract classes, but there are no ordinary member variables in interfaces
  3. An abstract class can contain non-abstract ordinary methods. All methods in an interface must be abstract, and there can be no non-abstract ordinary methods.
  4. The access type of the abstract method in the abstract class can be public, protected and (the default type, although no error is reported under eclipse, it should not work), but the abstract method in the interface can only be of the public type, and the default is public abstract Types of.
  5. The abstract class can contain static methods, but the interface cannot contain static methods
  6. Both abstract classes and interfaces can contain static member variables. The access type of static member variables in abstract classes can be arbitrary, but the variables defined in the interface can only be of public static final type, and the default is public static final type.

14. Session tracking

Session tracking is a flexible and portable mechanism that makes state programming on the Web possible.

HTTP is a stateless protocol. Whenever a user makes a request, the server will respond. The connection between the client and the server is discrete and non-continuous. When users switch between multiple pages of the same website, it is impossible to determine whether they are the same customer. Session tracking technology can solve this problem. When a client switches between multiple pages, the server saves the user's information.

There are four ways to implement session tracking technology : URL rewriting, hidden form fields, cookies, and sessions.

  1. Hidden form fields: very suitable for conversational applications that do not require a large amount of data storage.
  2. URL rewriting: URL can be appended with parameters and sent with the server request. These parameters are name/value pairs.
  3. Cookie: A cookie is a small, named data element. The server uses the SET-Cookie header to send it to the client as part of the HTTP response. The client is requested to save the Cookie value, and then uses a Cookie header to return it to the server in subsequent requests to the same server. Compared with other technologies, one advantage of Cookie is that it can retain its value after the browser session ends, even after the client computer restarts.
  4. Session: Use the setAttribute(String str,Object obj) method to bind the object to a session

Guess you like

Origin blog.csdn.net/qq_52212721/article/details/112545613