Always feel that the Java Foundation is good, but the first see (integer) 129 = = (integer) 129 expression can not immediately reflect the result is true or false, may wish to first look at the following simple Java program:
Package com.csdn.test;
public class Main {public
static void Main (string[] args) {
System.out.println ("(integer) 129 = = (integer) 129");
System.out.println ((integer) 129 = = (integer) 129);
System.out.println ("(integer) 127 = = (integer) 127");
System.out.println ((integer) 127 = = (integer) 127);
}
After the compilation runs, the console outputs the following results:
(integer) 129 = = (integer) 129
false
(integer) 127 = = (integer) 127
True
If the Java basic focus on less, may have two or three years of experience in Java programmers can not explain why there is such a difference, the author is also on the internet to find out some information to understand the ins and outs.
To understand this problem, first of all to master Java automatic Boxing, unpacking related knowledge, the Java automatic loading/unboxing occurred in operation and comparison operations, such as:
Integer a = ten;
A = a + 10; 1. The unboxing is int type 2. Calculates a+10 3. Boxing 20 to an integer type.
System.out.print (a > 10); 1. Remove a box as int type 2. Then compare
When you compare using = =, the situation is as follows:
If both sides are boxed types, the comparison reference points to the same object in the heap memory.
If there is a boxed type on either side of the = = and the other side is the base type, the boxed type is split into the base type and then compared. For example:
Integer a = new integer (129);
Integer b = new Integer (129);
System.out.println (A = = B); Compares reference types, returns false
System.out.println (A = = 129);//A for unboxing, base type comparison, return True
The problem is what is the difference between the value of the expression (integer) 127 = = (integer) 127 and (integer) 129 = = (integer) 129.
We may wish to look at the source code of the Java.lang.Integer class, as follows:
* * Copyright (c) 1994, 2013, Oracle and/or its affiliates.
All rights reserved. * ORACLE proprietary/confidential.
Use are subject to license terms.
* * Package Java.lang;
Import java.lang.annotation.Native; /** * The {@code Integer} class wraps a value of the primitive type * {@code int} in an object.
An object of type {@code Integer} * contains a single field whose type is {@code int}. * * <p>in addition, this class provides several methods for converting * a {@code int} to a {@code String} and a
{@code String} to a * {@code int}, as as as other constants and methods useful when * dealing with a {@code int}. * <p>implementation Note:the implementations of the "bit twiddling" * methods (such as {@link #highestOneBit (in T) Highestonebit} and * {@link #numberOfTrailingZeros (int) Numberoftrailingzeros}) are * Based on material from Henry S.
Warren, Jr. ' s <i>hacker ' s * delight</i>, (Addison Wesley, 2002). * @author Lee Boynton * @auThor Arthur van Hoff * @author Josh Bloch * @author Joseph D. Darcy * @since JDK1.0/Public final class Integer E Xtends number implements Comparable<integer> {... private static class Integercache {Stati
c Final int low =-128;
static final int high;
Static final Integer cache[];
static {//high value May is configured by property int h = 127;
String Integercachehighpropvalue = Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high"); if (integercachehighpropvalue!= null) {try {int i = parseint (Integercach
Ehighpropvalue);
i = Math.max (i, 127);
Maximum array size is integer.max_value h = math.min (i, Integer.max_value-(-low)-1); catch (NumberFormatException NFE) {//If The property cannot is parsed into INT, ignore it.
} high = h;
cache = new Integer[(high-low) + 1];
int j = Low;
for (int k = 0; k < cache.length; k++) cache[k] = new Integer (j + +);
range [ -128, 127] must be interned (JLS7 5.1.7) assert Integercache.high >= 127;
Private Integercache () {}} ...
}
Only a fraction of the key code is intercepted here, as shown in the above code, the Integer class only caches objects between -128~127, (integer) 127 = = (integer) 1272-side boxing, and actually points to the same object in the heap memory (integer) 129 = = (Integer) 129, boxed as a reference type, does not cache, pointing to different objects in the heap memory, so the comparison is false.