Explore the underlying details of comparison between java basic types and packaging types using the operator =

Source: Internet
Author: User

A Project Manager challenges whether a packaging object is compared with a value of the basic type, and whether it is a comparative value or a comparative reference. A project manager considers it to be a reference. The following practices are incorrect. But in fact it is a comparison value. Does the compiled jdk process the statements of our object and value table and compare the values of the basic type at the end? The code below is discussed. An Object value is taken out of the hibernate query using the unique method. In fact, this value is actually an Integer instance, so use the code similar to the test code that is converted first and then compared as the problem code we explored this time.
Test comparison code:
[Java]
Public class TestIntegerCompare {
Public static void main (String [] args ){
Object obj = new Integer (1024 );
If (Integer. valueOf (obj. toString () = 1024 ){
System. out. println ("bing go! Integer. valueOf (obj. toString () = 1024 is true ");
}
}
 
}

Jdk 1.6.34 java. lang. Integer. class source code:
[Java]
Public static Integer valueOf (String s, int radix) throws NumberFormatException {
Return Integer. valueOf (parseInt (s, radix ));
}
Public static Integer valueOf (String s) throws NumberFormatException {
Return Integer. valueOf (parseInt (s, 10 ));
}
Public static Integer valueOf (int I ){
Assert IntegerCache. high >=127;
If (I> = IntegerCache. low & I <= IntegerCache. high)
Return IntegerCache. cache [I + (-IntegerCache. low)];
Return new Integer (I );
}
Public static int parseInt (String s) throws NumberFormatException {
Return parseInt (s, 10 );
}
Public static int parseInt (String s, int radix)
Throws NumberFormatException
{
// Omitted a series of parameter detection
Int result = 0;
Boolean negative = false;
Int I = 0, len = s. length ();
Int limit =-Integer. MAX_VALUE;
Int multmin;
Int digit;
 
If (len> 0 ){
Char firstChar = s. charAt (0 );
If (firstChar <'0') {// Possible leading "+" or "-"
If (firstChar = '-'){
Negative = true;
Limit = Integer. MIN_VALUE;
} Else if (firstChar! = '+ ')
Throw NumberFormatException. forInputString (s );
 
If (len = 1) // Cannot have lone "+" or "-"
Throw NumberFormatException. forInputString (s );
I ++;
}
Multmin = limit/radix;
While (I <len ){
// Accumulating negatively avoids surprises near MAX_VALUE
Digit = Character. digit (s. charAt (I ++), radix );
If (digit <0 ){
Throw NumberFormatException. forInputString (s );
}
If (result <multmin ){
Throw NumberFormatException. forInputString (s );
}
Result * = radix;
If (result <limit + digit ){
Throw NumberFormatException. forInputString (s );
}
Result-= digit;
}
} Else {
Throw NumberFormatException. forInputString (s );
}
Return negative? Result:-result;
}

Let's run the test code. We can see that the statement block in the test code if can run and output text, however, since then, we still have doubts about the process of comparing an object with a value of the basic type. After compilation, jdk converts the int value to an Integer instance and compares it with an Integer instance, or vice versa. In this question, let's take a look at the javap. Input javap-v-l Test TestIntegerCompare. class to see the following decompiled jvm commands.


From the above jvm instructions, we can see that the code number 15: indeed, the valueOf method is called to parse the string as an Integer instance, but the subsequent number is 18: if the intValue () method of the Integer instance is called, the corresponding int value of the instance is returned. In this case, int and int are compared, so the result is true;
Through the above analysis, we can determine that the comparison of "Packaging object = corresponding value" is feasible, but it is not recommended. Because the String instance is parsed as an Integer instance, and the int value retrieved from the Integer instance is compared, this Integer instance has one more action, it is better to directly use parseInt to parse characters for direct comparison of corresponding int values, saving the resources wasted in creating an Integer instance.
Ps: if there is a problem with the above analysis, we hope that senior experts will correct it.
 

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.