Hello, I'm 200. I'm busy recently, so updates will be slower.
Introduction
Gold, nine silver and ten silver. Now we're ready for an interview. It's simple and rough. Topic ~
Interview Questions
1. What is the difference between StringBulider and StringBuffer and String?
StringBulider variable string threads are insecure and efficient
StringBuffer variable string thread security is inefficient
String immutable strings are used as they are and should not be used in loops because many unused strings result
⚠️ This part of the suggestion is to see the source code may understand more thoroughly, there will be time for source analysis oh~
2. Why is String not mutable?
String is immutable because it has been modified by final.
3. What does the final keyword do?
Final-modified variables, methods, classes, must be initialized in final-modified variables, cannot be changed after assignment, final-modified methods cannot be overridden, final-modified classes cannot be inherited final-modified variables must display initialization, and this variable cannot be changed by initialization.
4. Why can't it be changed?
Non-changeable means that the value is not changeable for the basic type, the reference is not changeable for the object variable, that is, it cannot point to other objects. If the final modifier is the object type, then non-changeable means that the change can no longer point to other objects, but the value of the object can be changed.
* final Operate operate = new Operate();
* operate.i=11;
* operate.i=12;
* System.out.println(operate.i);//Output 12
* These are custom classes, even arrays. List Saved values can also be changed.
5. What uses final modifiers in java
For example: Math In class double E double PI * In an interface, variables are public static final Modified
6. Distinguish final from finally and finalize()
finally
* finnally Is part of exception handling and can only be used with try/catch A statement block is included to indicate that the statement must be final
* Will be executed (whether or not an exception is thrown)
* finnally This will not be done as follows:
* public static int test(){
* int i = 1;
* // if (i==1)
* // {
* // return 0;
* // }
* System.out.println("111111111111");
* i = i/0;
* try {
* System.out.println("try");
* return i;
* }finally {
* System.out.println("finaly");
* }
* }
* public static void main(String[] args) {
* System.out.println("test"+test());
* }
* Exception in thread "main" java.lang.ArithmeticException: / by zero111111111111
at Main.test(Main.java:53)
at Main.main(Main.java:62)
*
When we remove the three lines of commented statements, the result is: test0
- Why is there no final statement executed in either case and what does it mean?
- finnlly statements will only execute quickly if the corresponding statement block is executed, both of which precede the execution of the tru e statement block
- An exception has been returned or thrown, so the final statement corresponding to try has not been executed
- But in some cases, even if the try statement executes, the finnally statement may not necessarily execute
For example:
public static int test(){
int i = 1;
// if (i==1)
// {
// return 0;
// }
// System.out.println("111111111111");
// i = i/0;
try {
System.out.println("try");
System.exit(0);
return i;
}finally {
System.out.println("finaly");
}
}
public static void main(String[] args) {
System.out.println("test"+test());//Output result:try
}
finnally statement block or not executed?
Because the exit(0) statement is executed in the try statement block to terminate the running of the Java virtual machine, the exit(0) method is not normally called, but when a thread is interrupted or terminated while executing the try or catch statement block, the corresponding finally may not execute.
finalize()
Finalize() is defined in java.lang.Object, that is, every object has this method, which is called when GC starts and the method is recycled.In fact, GC can recycle most of the objects. Every new object can be processed by gc. Generally, finalize () will not be used. In special cases, you need to release some resources when the object is recycled, such as socket links, which are created when the object is initialized and valid throughout its life cycle, then finalize () needs to be implemented.One thing to note when using finalize is to call super.finalize();The finalize() method of an object can only be called once, and a call to finalize() does not mean that GC will immediately recycle the object, so it is possible to call finalize() and the object will not need to be recycled, and then when it is really to be recycled, finalize() will not be called because it was called once before, causing problems.Therefore finalize() is not recommended.
End
That's all there is in this article. There will be time in the next period to supplement this one.👋