Comparison of integers and int

Source: Internet
Author: User

1, integer and int comparison

Let's take a look at some of the more interesting code:

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

If you can come up with the correct answer to this question, and understand the principles. It means you can base it. 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 the reference of two objects, here is the ABCD is a new object, should be entered false. This is the interesting point of this problem, whether it is the interview question or forum discussion area, the problem of the appearance rate is very high. The principle is actually very simple, let's go and see the Integer.java class.

public static Integer valueof (int i) {  
    return  i >= 128 | | I < -128? new Integer (i): 
    small_values[i + 1 ];  
}  

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 will be automatic boxing operation, simple point, that is, the basic data type into an integer object, and converted to an integer object is called the ValueOf method, you can see that the integer 128 to 127 cache down. The official explanation is that small numbers use a higher frequency, so in order to optimize performance, the number of these between the cache. That's why the answer to this question is false and ture. When the value of the declared integer object is between 128 and 127, the same object is referenced, so the result is true.

Let's look at a piece of code:

Integer a = new integer (1000);  
int b = 1000;  
Integer c = new Integer (a);  
Integer d = new Integer (a);  
System.out.println (A = = B);  
System.out.println (c = d);  

This question is following the previous question, if this question you can arrive very quickly the answer, then congratulates you, = = The comparison character you even if grasps quite thoroughly.

Correct answer: TRUE, False

See this answer a lot of small partners will be puzzled, first of the second, according to the first question, the integer is not 128 to 127 cache up it. This is not supposed to be true, but you look closely, the integer here is our own new, not the cache, so the result is false.

Now let's see why the first one is 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 the cache, a is the new object, which is supposed to be false. But note that b here is the int type. When the int and the integer are compared, Java will automatically disassemble the integer, that is, the integer to type int, so this is compared to the value of type int, so the result is true.

2, String

Read a piece of 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 syntax of = =, first s1, S2, S3 are three different objects, in common sense, the output will be false. However, the running result of the program is true and false.

The second output false can be understood, and the first output 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 in the heap memory the new objects and arrays are stored.

However, there is also a region called a constant pool.

Like we usually want string s1 = "abc"; A declared string object, whose value is stored in a constant pool. After we create an object such as String S1 = "abc", "ABC" is stored in the Chang (also known as a string pool), and when we create a reference to string s2 = "abc", the Java bottom takes precedence over the constant pool for the existence of "ABC", If present, let S2 point to this value and will not re-create it if there is no pool in the constant pool that was created and added. That's why the answer is true and false.

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.