1000 100 比較問題

//  正常情況應該是兩個比較都是false  爲什麼 會100 會出現false呢  只是因爲 jdk 會緩存 -127 128 之間的整數 這時的比較就是兩個緩存的值得比較  



public static void main(String[] args) {

String d =tsetString();

System.out.println(d);  // true

String f =tsetString2(); //false

System.out.println(f);

}

public static String tsetString(){

               Integer a = 1000;

               Integer b = 1000;

               String c = " ";

               if(a==b){

               c = "true";

               }

               else{

               c = "false";

               }

               return c;

}

public static String tsetString2(){

        Integer a = 100;

        Integer b = 100;

        String c = " ";

        if(a==b){

        c = "true";

        }

        else{

        c = "false";

        }

        return c;

}

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