Home » Java » Four Java based questions

Four Java based questions

, = = symbol

first, look at a more interesting code,

Integer, a = 1000, b=1000;
Integer C = 100, d=100;
System.out.println (a==b);
System.out.println (c==d);

First published under the answer, running the code, we have false true. we know is = two object references, where ABCD is the new object out, arguably should be to enter the false. This is interesting about this problem, whether the interview questions or discussion forum, the rate of appearance this problem is very high. The principle is very simple, we go to look at the Integer.java class come.

public, static, Integer, valueOf (int, I) {
Return I I < -128 > = 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 a Integer C = 100; when will carry out automatic packing operation. At this time, simply put, is the basic data type is converted into a Integer object, and convert the Integer object valueOf method is invoked, as you can see, Integer in the -128-127 cache down. The official explanation is a small number of the frequency of use is relatively high, so in order to optimize the performance, the number between the cache down. This is why the answer to the question about false and ture. When the statement of the value of the Integer object in the -128-127 between the time, refer to the same object, so the result is true.

two, String

Next, 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);

According to the grammar of S1 = S2, S3, first of all, are three different objects, common sense, the output will be false. but the result of program is true, false. second false output can be understood, the first output true and confusing. We know that the reference variable and object types the distribution function is in the stack and heap memory, the object is stored in the new and out of the array. However, in addition to an area called the constant pool. Like we usually want to String S1 = "ABC"; so that the string object, its value is stored in the constant pool. When the after we create String S1 = "ABC" as an object, "ABC" is stored in the constant pool (also known as the string pool), when we create a reference String
When S2 = "ABC", Java will first search the bottom in the constant pool in the existence of "ABC", if there is to point to this S2 value, will not be re created if the constant pool does not create and add the pool. This is why the answer is.
true and false

three, final keyword

Or a piece of code,

, public, void, mRun (final, String, name) {
New, Thread () {
Public, void, run () {
Thread.sleep (1000);
} catch (InterruptedException, e) {
E.printStackTrace ();
System.out.println (name);
}.start ();

We believe that this code has written many, when the internal access local variables, need to add the final modifier in a local variable, otherwise the compiler will usually do. Well, the second question is, why should add final modifiers? I believe most buddy never think about this problem when, whenever the use of the direct plus you have never go to the bottom of the principle. It is not advisable for a good programmer, we should not only know its not to know its the reason why.

Now we analyze, why should add the final keyword. The inner class is a member of the life cycle level, while the local variables of the life cycle is like method. That is to say there will be such a situation, when the mRun method is executed, the thread running new, a new thread will sleep inside the main thread seconds. MRun will continue to perform, the execution is completed, the end of the name attribute life cycle. Second, Syetem.out.printh (name). But this time name has come to an end, not in the memory of.Java is to put an end to this error, strict access to local variables within the class, must be modified using the final keyword. After local variables are modified by final, at this time will have a local variable copy in memory, when the internal access is actually access the copy. It is as if the local variables of the life cycle is long In the final analysis, or Java engineers in advance to fill out this hole for us, otherwise do not know how many small partners will be internal class variables and worry about.

four, Integer and int, those things,

Look at the 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);

The correct answer: true, false

See the answer a lot of buddy and wonder, first is the two Integer, according to the first question is not the -128-127 cache up? This is not should be true, but if you look carefully, here is our own Integer new, and not with the cache, so the result is now the first false. why is true? The first value is 1000, and certainly Never mind we know. Since the Integer cache and cache Never mind, a is a new new, arguably the input should be false to B. But note that this is int type. When the int and Integer time = = comparison, Java will take Integer automatic unpacking, Integer is to turn into a int type, so here is the comparative value of type int, so the result is true.