Long型比較是否相等的四個方法

		Long a = 200L;
        Long b = 200L;
        System.out.println(a.equals(b));
        System.out.println(a.longValue()==b.longValue());
        System.out.println(a-b==0);
        System.out.println(a.compareTo(b) == 0);

共四種方法,前兩種較爲常用。
注意如果比較的值在-128到127之前,可以用==比較。

基本類型long,int等都有常量池,無論大小,都可以用==比較

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章