This is the 9th day of my participation in the More text Challenge. For details, see more text Challenge
Java error point 2
If have the word of understanding error, implore everybody to point out!!
Integer
-
== : For variables of basic data type, **==** is a direct comparison of their values. For variable objects that reference data types, it is a comparison of their memory addresses.
-
The most common use of equals is to compare values. The reason for this is that equals can be overridden. For example, Intenger equals is a comparison of values, whereas Object equals is a comparison of ==.
See the source code for Object
public boolean equals(Object obj) { return (this == obj); } Copy the code
Now look at the source code for Integer
public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; } Copy the code
Take a look at the source code for String
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while(n-- ! =0) { if(v1[i] ! = v2[i])return false; i++; } return true; }}return false; } Copy the code
The sample code
package com.wangscaler;
public class TestInteger {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
Integer bb = new Integer(127);
int c = 127;
Integer d = 128;
Integer e = 128;
int f = 128; System.out.println(a.equals(b)); System.out.println(a == b); System.out.println(b == bb); System.out.println(a.equals(c)); System.out.println(a == c); System.out.println(d.equals(e)); System.out.println(d == e); System.out.println(d.equals(f)); System.out.println(d == f); }}Copy the code
The execution result
true
true
false
true
true
true
false
true
true
Copy the code
The principle of
The basic data types are stored as constants in the constant pool in the method area under a HashSet policy. A constant will only correspond to one address. As we wrote in memory in the previous section, the string “wang” and the name value “wang” in the people object refer to the same address. Therefore, basic data types and String constants can be directly compared by using ==.
However, why is the value of Integer 127 when system.out.println (a == b); System.out.println(d == e); Is it not equal? Confused, I opened the source code for open Integer and finally found the problem.
The source code is as follows:
//IntegerCache.low==-127
//IntegerCache.high==128
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Copy the code
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if(integerCacheHighPropValue ! =null) {
try {
int i = parseInt(integerCacheHighPropValue);
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 be parsed into an 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(a) {}}Copy the code
When the value of an Integer is between -127 and 128, it is fetched in the array (the pool of constants mentioned above) by IntegerCache. If the value of an Integer is outside the range, it is new
System.out.println(b == bb); The result of is false.
Equals () is a comparison of values, so it makes sense that equals is true.
Conversion from decimal to binary
1, take the mod method
2, quick method
A switch statement
Each case statement needs to end with a break; (Note: Huawei’s programming specifications emphasize that 1, must have the code of default statement (except enum) 2, if the enum does not write default, must write all cases of enum)
Error sample code
package com.wangscaler;
public class TestSwitch {
public static void main(String[] args) {
TestSwitch testSwitch =new TestSwitch();
testSwitch.typePeople(People.MAN);
}
enum People {
MAN, WOMEN, OTHER
}
private void typePeople(People people) {
switch (people) {
case MAN:
System.out.println("This is a Man");
case WOMEN:
System.out.println("This is a Women");
case OTHER:
System.out.println("I do not Known"); }}}Copy the code
The execution result
This is a Man
This is a Women
I do not Known
Copy the code
why
Swith terminates when there is a break, and if there is no break, the swith statement is executed down from the case that was executed. Instead, add a break at the end of each case statement;