For example:
//Test004.javapublic class Test004 { public static void main(String[] args) { int a =8; int b =8; if(a==b){ System.out.); } else{ System.out.println("a is not equal to b."); } /* * == and equals *The Operator == works a bit different on objects than on primitives. When we are using objects and want to check if they are equal, the operator == wiil say if they are the same, if you want to check if they are logically equal, you should use the equals method on the object. For example: */ String person_a=new String("gavin"); String person_b=new String("gavin"); String person_c=person_a; System.out.println(person_a==person_b);//這里會輸因為false, //因為person_a和person_b不是一個對象,分配的物理空間的地址不一樣 System.out.println(person_a.equals(person_b));//這里會輸出true, //因為person_a和persion_b的邏輯值一樣,都是gavin System.out.println(person_a==person_c);//這里會輸出true, //因為person_a和person_b實際上是同一個對象 }}
上述代碼中的第6行中,a=b就是條件句。在java中,條件句返回的結果必須是boolean類型的值。
新聞熱點
疑難解答