為什麼java中要做IntegerCache這種設定?

時間 2021-05-29 22:49:56

1樓:Night Silent

所以 Integer 之類的包裝類都實現了 Comparable 介面啊,使用 compareTo() 比較 Integer 就妥妥的: )

如果不按照設計意圖,而使用 == 比較,那就是坑了。

/*** Compares two objects numerically.

** @param anotherInteger the to be compared.

* @return the value if this is

equal to the argument ; a value less than

code 0} if this is numerically less

than the argument ; and a value greater

than if this is numerically

greater than the argument (signed

comparison).

* @since 1.2

*/public

intcompareTo

(Integer

anotherInteger)

為什麼java中要分public protected 和 private?

你要知道,人類的智商是不足以支撐一蹴而就的。因此世間的大部分事物都是一步一步曲折發展的,這是本因。這個本因推動了管理學的發展,管理學再提出許可權控制的思想,想到這一層,我想你也就沒什麼疑問了。 迷茫的小學生 這個就是物件導向的封裝。由於現在開發,都是MVC架構,Model層寫上實體,寫上privat...

為什麼java中宣告多用Map,List而不是具體實現型別?

比如介面的返回值,外部需要key,value 型別的資料結構,那就返回Map。至於你在實現的時候,如果沒有特殊需求,就用HashMap,高效讀寫 如果你需要排序,就來個TreeMap。但是對呼叫的人來說都是get key 或者用迭代器。 舉例List list new ArrayList 當我發現對...

Java中foreach為什麼不能給陣列賦值?

顏欽能 給你舉乙個例子,你看一下可能比較能理解 for int element array1 question 為什麼用Foreach無法給陣列賦值element r.nextInt 101 1 System.out.println element 雖然可以輸出十個隨機數,但並沒有把十個隨機數賦給a...