Four Java basic questions

Source: Internet
Author: User

The use of a = = character

First look at a more interesting code

   
  
  1. Integer a = 1000,b=1000;
  2. Integer c = 100,d=100; public void mRun(final String name){
  3. new Runnable() {
  4. public void run() {
  5. System.out.println(name);
  6. }
  7. };
  8. }
  9. System.out.println(a==b);
  10. System.out.println(c==d);

If you can get the right answer to this question, and you can understand the principle. It means you can do the basics. If your answer is true and true, you have a lack of foundation.

First publish the answer, run the code and we'll get false true. We know that = = comparison is a reference to two objects, where ABCD is the newly created object, it should be said to enter false. This is the interesting point of the problem, whether it is the interview or the forum discussion area, the problem of the appearance rate is very high. The principle is actually very simple, we go to see the Integer.java this class to know.

  
  
 
  1. public static Integer valueOf(int i) {
  2. return i >= 128 | | I < - 128 new integer ( i : small_values [ i + 128
  3. }
  4. /**
  5. * A cache of instances used by {@link Integer#valueOf(int)} and auto-boxing
  6. */
  7. private static final Integer[] SMALL_VALUES = new Integer[256];
  8. static {
  9. for ( int i = - 128 ; I < 128 ; I ++) {
  10. SMALL_VALUES[i + 128] = new Integer(i);
  11. }
  12. }

When we declare an integer c = 100; In this case, the automatic boxing operation, simple point, that is, the basic data type conversion to an integer object, and converted to an integer object is called the ValueOf method, you can see that the integer 128-127 cache down. The official explanation is that small numbers are used more frequently, so in order to optimize performance, the number is cached. That's why the answer to this question is false and ture. When the value of the declared integer object is between-128-127, the same object is referenced, so the result is true.

Second, String

Then look at the code

   
  
  1. String s1 = "abc";
  2. String s2 = "abc";
  3. String s3 = new String("abc");
  4. System.out.println(s1 == s2);
  5. System.out.println(s1 == s3);

Let's guess what the answer to this question is?

According to the syntax of = =, first s1, S2, S3 is three different objects, in common sense, the output will be false. However, the running result of the program is true, False. The second output, false, is understandable, and the first output of true is confusing. We know that some basic types of variables and object reference variables are allocated in the stack memory of the function, while the heap memory holds the new object and array. However, there is another area called a constant pool. Like we usually want string s1 = "abc"; This declares a string object whose value is stored in a constant pool. When we create an object such as String S1 = "abc", "ABC" is stored in Chang (also called a string pool), and when we create a reference to string s2 = "abc", the Java bottom will first look for "ABC" in the Constant pool, If present, let S2 point to this value and will not be recreated if there is no pool in the constant pool that was created and added. This is the reason why the answer is true and false.

Third, Final keywords

Or see a piece of code

   
  
  1. public void mRun(final String name){
  2. new Runnable() {
  3. public void run() {
  4. try {
  5. Thread.sleep(1000);
  6. } catch (InterruptedException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. System.out.println(name);
  11. }
  12. }.start();
  13. }

This kind of code is believed to have written many, when the internal class accesses the local variable, needs to add the final modifier before the local variable, otherwise the compiler will error. We usually do the same thing. OK, the second question is, why do you want to add the final modifier? I believe most of the small partners have not thought about this problem, whenever the use of the time, directly add to it, never to delve into the principles. This is not for a good programmer, we need not only know it but also know why.

Now let's analyze why we need to add the final keyword. First, the life cycle of the inner class is the member level, and the life cycle of the local variable is the method body. That is to say, when the Mrun method executes, new threads run, and a second is in the thread. The main thread continues execution, and the Mrun execution completes with the Name property end of the life cycle. After 1 seconds, Syetem.out.printh (name) executes. However, the name is now dead, not in memory. Java is to eliminate this error, strict requirements for internal classes in the local variables, you must use the final keyword decoration. After a local variable is final modified, it retains a locally-made copy in memory, which is actually accessed when the inner class accesses it. It is as if the life cycle of the local variable is getting longer. After all, the Java Engineer in advance to the pit to fill us, or do not know how many small partners will be for internal class local variables and worry about.

Iv. integers and int those things

Look at the code below

    
  
 
  1. Integer a = new Integer(1000);
  2. int b = 1000;
  3. Integer c = new Integer(10);
  4. Integer d = new Integer(10);
  5. System.out.println(a == b);
  6. System.out.println(c == d);

This problem is the follow-up of the first question, if you can quickly get the answer to the question, then congratulate you, = = Comparison you even grasp the more thorough.

------------------------------------------------------Split Line---------------------------------------------------------------- -----------

Correct answer: TRUE, False

See this answer a lot of small partners will be puzzled, first of all, the second, according to the first question, the integer is not 128-127 cache up? This is not supposed to be true, but if you look closely, the integer here is our own new, not the cache, so the result is false. Now, why is the first one true again? The first value here is 1000, which is definitely not related to the integer cache we know. Since it has nothing to do with caching, A is the new object, which is supposed to be a false input. But note b here is the int type. When int and integer do = = comparison, Java will automatically unboxing the integer, that is, the integer to the int type, so here is to compare the value of the int type, so the result is true.




From for notes (Wiz)

Four Java basic questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.