【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 ,函數方法名必須相同,看參數列表便可,無關返回值。
相關文章
相關標籤/搜索