Java中"=="與equals()方法的區別

1.  操作符”==”用來比較兩個操作元是否相等,這兩個操作元既可以是基本類型,也可以是引用類型。

例如:int a1=1,a2=3;

      boolean b1=a1==a2; //”==”的操作元爲基本類型,b1值爲false

      String str1=”Hello”,str2=”World”;

      boolean b2=str1==str2; //”==”的操作元爲引用類型,b2的值爲false

 

2.  當操作符“==”兩邊都是引用類型變量時,這兩個引用變量必須都引用同一個對象,結果才爲true,否則爲false.

   如:class IntegerMethod {

    void method() {

       Integer int1 = new Integer();

       Integer int2 = new Integer();

       Integer int3 = int1;

 

       System.out.println("int1==int2 is " + (int1 == int2));

       System.out.println("int1==int3 is " + (int1 == int3));

       System.out.println("int2==int3 is " + (int2 == int3));

    }

}

 

public class Integer {

 

    public static void main(String[] args) {

       IntegerMethod im = new IntegerMethod();

       im.method();

    }

 

}

 結果爲:int1==int2 is false

int1==int3 is true

int2==int3 is false

 

3. 當“==”用於比較引用類型變量時,“==”兩邊的變量被顯式聲明的類型必須是同種類型或有繼承關係。

 class Creature {

    Creature() {

 

    }

}

 

class Animal extends Creature {

    Animal() {

 

    }

}

 

class Dog extends Animal {

    Dog() {

 

    }

}

 

class Cat extends Animal {

    Cat() {

 

    }

}

 

public class IntegerTest {

 

    public static void main(String[] args) {

       Dog dog = new Dog();

       Creature creature = dog;

       Animal animal = new Cat();

       System.out.println(dog == animal);

       System.out.println(dog == creature);

 

    }

}

結果爲:false

true

 

4.Object類的equals()方法比較規則爲:當參數obj引用的對象與當前對象爲同一個對象時,就返回true,否則返回false.兩者的區別是“==”比較的是變量的值(也即爲內存地址),而equals()比較的是變量的內容。

class ConInteger {

    Integer int1 = new Integer(1);

    Integer int2 = new Integer(1);

 

    String str1 = new String("Hello");

    String str2 = new String("Hello");

 

    void print() {

       System.out.println(int1 == int2);

       System.out.println(int1.equals(int2));

 

       System.out.println(str1 == str2);

       System.out.println(str1.equals(str2));

    }

}

 

public class TestInteger {

    public static void main(String[] args) {

       new ConInteger().print();

    }

}

結果爲:false

true

false

true

 

5.只要兩個對象都是同一個類的對象,並且它們變量屬性相同,則結果爲true,否則爲false.

class TestInteger{

    private String name;

    TestInteger(String name){

       this.name=name;

    }

   

    boolean equal(Object o){

       if(this==o){

           return true;

       }

       if(!(o instanceof TestInteger)){

           return false;

       }

       final TestInteger other=(TestInteger)o;

       if(this.name.equals(other.name)){

           return true;

       }

       else{

           return false;

       }

    }

}

 

public class TestIntegerOne {

    public static void main(String[] args) {

       TestInteger person1=new TestInteger("Tom");

       TestInteger person2=new TestInteger("Tom");

      

       System.out.println(person1==person2);

       System.out.println(person1.equal(person2));

    }

}

結果爲:false

true

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