我直接写内容;
int a[]= new int[4];
int b[]= new int[4];
a.equals(b); false???? 第一个不懂为什么是FALSE
int a[]= new int[]{1,2,3,4};
int b[]= new int[]{1,2,3,4};
a.equals(b); false ???? 还是不懂为什么是FALSE
帮帮的谢谢了
int a[]= new int[4];
int b[]= new int[4];
a.equals(b); false???? 第一个不懂为什么是FALSE
int a[]= new int[]{1,2,3,4};
int b[]= new int[]{1,2,3,4};
a.equals(b); false ???? 还是不懂为什么是FALSE
帮帮的谢谢了
你觉得会是一个东西吗?
int b[]= new int[4];
初始化一个int数组将引用赋予a,b
a.equal(b)实际上比较的a,b数组的引用,也就是比较它们在堆内存的位置
int a[]= new int[]{1,2,3,4};
int b[]= new int[]{1,2,3,4};
道理也是一致的,只是初始化了数组里每个元素的值
public boolean equals(Object obj) {
return (this == obj);
}
因为a != b 所以false
没有重写过equals()方法的时候,它的含义是比较两个变量是否指向同一个对象的。
第二个equals是对字符串的比较啊。
int b[] = new int[4];
// a.equals(b); false???? 第一个不懂为什么是FALSE
int a1[] = new int[] { 1, 2, 3, 4 };
int b1[] = new int[] { 1, 2, 3, 4 };
// 因为它们都是不同的对象,如果需要进行数组的比较需要用到Arrays.equals()方法
System.out.println(Arrays.equals(a, b));
System.out.println(Arrays.equals(a1, b1));
int b[]= new int[4];
a.equals(b); false???? 第一个不懂为什么是FALSE //equals不适用于基本类型,即int,float之类的,你可以用Integer,String等创建对象。
int a[]= new int[]{1,2,3,4};
int b[]= new int[]{1,2,3,4};
a.equals(b); false ???? 还是不懂为什么是FALSE //同上
首先看hashcode是否相等,如果不相等就是false,否则再看是否是同一个内存对象(这里是没有重写的情况)
为什么楼上这么多人说有呢?
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The <code>equals</code> method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* <code>x</code>, <code>x.equals(x)</code> should return
* <code>true</code>.
* <li>It is <i>symmetric</i>: for any non-null reference values
* <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
* should return <code>true</code> if and only if
* <code>y.equals(x)</code> returns <code>true</code>.
* <li>It is <i>transitive</i>: for any non-null reference values
* <code>x</code>, <code>y</code>, and <code>z</code>, if
* <code>x.equals(y)</code> returns <code>true</code> and
* <code>y.equals(z)</code> returns <code>true</code>, then
* <code>x.equals(z)</code> should return <code>true</code>.
* <li>It is <i>consistent</i>: for any non-null reference values
* <code>x</code> and <code>y</code>, multiple invocations of
* <tt>x.equals(y)</tt> consistently return <code>true</code>
* or consistently return <code>false</code>, provided no
* information used in <code>equals</code> comparisons on the
* objects is modified.
* <li>For any non-null reference value <code>x</code>,
* <code>x.equals(null)</code> should return <code>false</code>.
* </ul>
* <p>
* The <tt>equals</tt> method for class <code>Object</code> implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values <code>x</code> and
* <code>y</code>, this method returns <code>true</code> if and only
* if <code>x</code> and <code>y</code> refer to the same object
* (<code>x == y</code> has the value <code>true</code>).
* <p>
* Note that it is generally necessary to override the <tt>hashCode</tt>
* method whenever this method is overridden, so as to maintain the
* general contract for the <tt>hashCode</tt> method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return <code>true</code> if this object is the same as the obj
* argument; <code>false</code> otherwise.
* @see #hashCode()
* @see java.util.Hashtable
*/
public boolean equals(Object obj) {
return (this == obj);
}
2、Object类的equals()方法,实质上还是使用==比较对象。
3、JDK中很多类重写了equals()方法,包括java.lang.String类和java.util.Arrays类。
4、所以,当你比较两个字符串的时候,它使用的是String类下的equals()方法,这个方法比较的是对象值。
5、同样的道理,如果需要比较两个数组的值,就需要使用Arrays类中的equals()方法。
6、代码示例:int[] a = new int[]{1,2,3};
int[] b = new int[]{1,2,3};
System.out.println(Arrays.equals(a , b));结果返回true。
所以两个比较结果都是false
数组的equals不像字符串那样重写过基类的equals,你可以用Arrays.equals(a , b)比较数组的内容
2.String和Arrays类的equals是重写(overridden)过的,比较的是内容,而不是地址
第二个错误也一样。
就是STRING 类的equals方法比较的是内容,而其他的没有重写的就是比较的应用地址。你的a和b在内存中存放的地址不一样(不管值是否一样),当然就是FALSE了,
两个对象的空间不同所以是FALSE
如果比较的是里面的值就是true
这是Object下的equals:
boolean equals(Object obj)
指示其他某个对象是否与此对象“相等”。
这是Arrays下的equals:
static boolean equals(boolean[] a, boolean[] a2)
如果两个指定的 boolean 型数组彼此相等,则返回 true。
static boolean equals(byte[] a, byte[] a2)
如果两个指定的 byte 型数组彼此相等,则返回 true。
static boolean equals(char[] a, char[] a2)
如果两个指定的 char 型数组彼此相等,则返回 true。
static boolean equals(double[] a, double[] a2)
如果两个指定的 double 型数组彼此相等,则返回 true。
static boolean equals(float[] a, float[] a2)
如果两个指定的 float 型数组彼此相等,则返回 true。
static boolean equals(int[] a, int[] a2)
如果两个指定的 int 型数组彼此相等,则返回 true。
static boolean equals(long[] a, long[] a2)
如果两个指定的 long 型数组彼此相等,则返回 true。
static boolean equals(Object[] a, Object[] a2)
如果两个指定的 Objects 数组彼此相等,则返回 true。
static boolean equals(short[] a, short[] a2)
如果两个指定的 short 型数组彼此相等,则返回 true。
是不一样的。
int b[] = new int[4];
// a.equals(b); false???? 第一个不懂为什么是FALSE
int a1[] = new int[] { 1, 2, 3, 4 };
int b1[] = new int[] { 1, 2, 3, 4 };
// 因为它们都是不同的对象,如果需要进行数组的比较需要用到Arrays.equals()方法
System.out.println(Arrays.equals(a, b));
System.out.println(Arrays.equals(a1, b1));
这个事正确的,因为你用了2个new创建了2个数组a和b,a.equals(b)是指这2个数组的内存地址相同,有点像c中指向同一地址的指针,Arrays.equals(a, b)是指你创建的数组a和b数值是否相等,返回的是Boolean型变量
如果没有重写 依然比较的是地址
public static void main(String[] args) { int a[]= new int[]{1,2,3,4};
int b[]= new int[]{1,2,3,4};
System.out.println("a.hashCode()"+a.hashCode());
System.out.println("b.hashCode()"+b.hashCode());
}}
用这段代码自己测试一下就行了
hashcode是不一样的!
想比较 需要,根据自己的需要自己写就重写 equals 和 hashCode 方法
Arrays.equals(byte[]a,byteb);
Arrays.equals(byte[]a,byte[] b);
public static void main(String[] args){
int a[]= new int[]{1,2,3,4};
int b[]= new int[]{1,2,3,4};
System.out.println(a.equals(b));
System.out.println(a.equals(b));
System.out.println(a);
System.out.println(b);
}
}
执行一下就知道了
如果重写了equals方法,没有重写hashCode方法,就会出现两个相同的对象,却具有不同的hash码的矛盾,所以重写了equals方法,一般都要重写hashCode方法,但不是一定要重写的,如果这个对象根本用不到hash码,就没必要重写。
equals方法返回false,hashCode的值有可能一样,也有可能不一样,但equals方法如果返回true,hashCode方法一定也要返回相同的值
在java中数组的比较是java.util.Arrays中的类方法,你应该吧你的代码改成如下的才对int a[]= new int[4];
int b[]= new int[4];
boolen frag=Arrays.equals(a,b);
int a[]= new int[]{1,2,3,4};
int b[]= new int[]{1,2,3,4};
boolen frag=Arrays.equals(a,b);这个才是比较数组的数值是否相等,你用的是比较内存地址是否相等
所有对象默认继承Object类,在没有重写equals方法时,执行的是Object对象的equals方法,String类重写了equals比较的是字符串内容。
Other: equals比较的是内存地址。
今天刚好看了String中equals的方法的介绍,书上说是比较内容而非地址("=="才是比较地址)。
原来object的equals是等同于==的。学习了~