【323天】我爱刷题系列082(2017.12.25)

叨叨两句

  1. ~

牛客网——java专项练习061

1

如下代码执行的结果显示是多少( )?
java

正确答案: B
A good and abc
B good and gbc
C test ok and abc
D test ok and gbc数组

public class Test03 {
 
     
    public static void main(String[] args) {
        
        Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
 
          
        System. out.println( f1 == f2); //true
        System. out.println( f3 == f4); //false
    }
}

当咱们给一个Integer赋予一个int类型的时候会调用Integer的静态方法valueOf。 
Integer f1 = Integer.valueOf(100); 
Integer f2 = Integer.valueOf(100); 
Integer f3 = Integer.valueOf(150); 
Integer f4 = Integer.valueOf(150); 
思考:那么Integer.valueOf()返回的Integer是否是是从新new Integer(num);来建立的呢?若是是这样的话,那么== 比较返回都是false,由于他们引用的堆地址不同。

具体来看看Integer.valueOf的源码


public static Integer valueOf(int i) {
         
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
}

在IntegerCache中cache数组初始化以下,存入了-128 - 127的值


cache = new Integer[(high - low) + 1];
int j = low;
for( int k = 0; k < cache.length ; k ++)
    cache[k] =
    cache[k] = new Integer(j ++);
    
从上面咱们能够知道给Interger 赋予的int数值在-128 - 127的时候,直接从cache中获取,这些cache引用对Integer对象地址是不变的,可是不在这个范围内的数字,则new Integer(i) 这个地址是新的地址,不可能同样的.
参考连接:http://blog.csdn.net/q3838418/article/details/77577490

2

下面哪几个函数 public void example(){....} 的重载函数?()函数

正确答案: A D
A public void example(int m){...}
B public int example(){..}
C public void example2(){..}
D public int example(int m,float f){...}spa

AD ,函数方法名必须相同,看参数列表便可,无关返回值。