How many of the four basic Java questions can you tell?
1. Use of == symbol
First, look at a more interesting code
Integer a = 1000,b=1000; Integer c = 100,d=100; public void mRun(final String name){ new Runnable() { public void run() { System.out.println(name); } }; } System.out.println(a==b); System.out.println(c==d); If you can get the correct answer to this question and understand the principle. It means that your basics are OK. If your answer is true and true, your foundation will be lacking.
First, publish the answer, run the code, and we will get false true. We know that == compares the references of the two objects. The abcd here is both newly created objects. In theory, false should be entered. This is the interesting thing about this question. Whether it is the interview question or the forum discussion area, the appearance rate of this question is very high. The principle is actually very simple. Let’s look at the Integer.java class and you will understand it.
public static Integer valueOf(int i) { return i >= 128 || i < -128 ? new Integer(i) : SMALL_VALUES[i + 128]; } /** * A cache of instances used by {@link Integer#valueOf(int)} and auto-boxing */ private static final Integer[] SMALL_VALUES = new Integer[256]; static { for (int i = -128; i < 128; i++) { SMALL_VALUES[i + 128] = new Integer(i); } } When we declare an Integer c = 100;. At this time, the automatic boxing operation will be performed. To put it simply, it means converting the basic data type into an Integer object, and converting it into an Integer object is the valueOf method called. You can see that -128-127 is cached in Integer. The official explanation is that small numbers are used more frequently, so in order to optimize performance, the numbers between them are cached. This is why the answer to this question is false and ture. When the declared Integer object's value is between -128-127, the same object is referenced, so the result is true.
2. String
Then look at the code
String s1 = "abc"; String s2 = "abc"; String s3 = new String("abc"); System.out.println(s1 == s2); System.out.println(s1 == s3); Let’s guess what the answer to this question is?
According to the syntax of ==, first of all, s1, s2, and s3 are three different objects. Common sense, the output will all be false. However, the running results of the program are indeed true and false. The second output false is understandable, and the first output true is puzzling again. We know that some basic type variables and reference variables of object are allocated in the function's stack memory, while new objects and arrays are stored in the heap memory. However, in addition to this, there is another area called a constant pool. Like we usually want String s1 = "abc";, the value of the string object declared is stored in a constant pool. When we create an object like String s1 = "abc", "abc" is stored in the constant pool (also called a string pool). When we create a reference String s2 = "abc", the Java underlying layer will prioritize finding whether "abc" exists in the constant pool. If it exists, let s2 point to this value, and will not recreate it. If there is no constant pool, it will be created and added to the pool. That's why the answers are true and false.
3. Final keyword <br /> Let’s look at a piece of code
public void mRun(final String name){ new Runnable() { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(name); } }.start(); } I believe everyone has written a lot of this kind of code. When internal classes access local variables, they need to add a final modifier before the local variables, otherwise the compiler will report an error. Usually we do the same. OK, the second question is, why add the final modifier? I believe most of my friends have never thought about this issue. Whenever they use it, just add it directly and have never delved into the principles. This is not desirable for an excellent programmer. We must not only know the truth but also the reason.
Now let’s analyze why you need to add final keyword. First of all, the life cycle of the internal class is at the member level, while the life cycle of local variables is in the method body. In other words, this situation will occur. When the mRun method is executed, the new thread will run and the new thread will sleep for a second. The main thread will continue to execute, mRun is executed, and the name attribute life cycle ends. After 1 second, Syetem.out.printth(name) is executed. However, at this time, name has died and is no longer in memory. Java is to eliminate this error and strictly requires local variables in internal classes to be modified with final keywords. After the local variable is modified by final, a local replica will be retained in memory. When the internal class accesses, it is actually accessed. This is like making the life cycle of a local variable longer. After all, it was the Java engineer who filled in this pit for us in advance, otherwise I wonder how many friends would worry about internal local variables.
4. Integer and int
Look at the following code
Integer a = new Integer(1000); int b = 1000; Integer c = new Integer(10); Integer d = new Integer(10); System.out.println(a == b); System.out.println(c == d);
This question is a follow-up to the first question. If you can get the answer to this question quickly, then congratulations, even if you have mastered the == Comparison function more thoroughly.
Reveal the correct answer: true, false
Many friends are puzzled after seeing this answer. Let’s talk about the second one first. According to the first question, hasn’t Integer cached -128-127? This should be true, but if you look closely, the Integer here is created by us, not using cache, so the result is false. Now let’s see why the first one is true again? First of all, the value here is 1000, which definitely has nothing to do with the Integer cache as we know it. Since it has nothing to do with cache, a is the object from the new new, the input should be false. But note that b is the int type here. When int and Integer compare ==, Java will automatically unbox Integer, that is, convert Integer into int type, so the value of int type is compared here, so the result is true.
After doing a few questions correctly, hurry up and check for omissions and fill in the gaps according to your own test level!