java中equal()方法和==方法的區別 – JAVA編程語言程序開發技術文章

對於基本數據類型而言。int a = 10;int b = 10; a==b 返回true.對於引用類型而言String c = new String("10");String d = new String("10");c == d 返回false,c.equals(d)返回true.

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.

兩個對象的hashCode相同,equal方法返回true.

對於兩個對象而言,==比較兩個引用的地址是否相同。 

摘自  行者無涯

發佈留言