Can someone explain why A.equals(B)there is falsewhen I start Busing int[] B = A.clone()BUT trueif I initiate Busing int[] B = A?
A.equals(B)
false
B
int[] B = A.clone()
true
int[] B = A
int[] A = {1, 2, 3, 4, 5}; int[] B = A; //int[] B = A.clone(); if(A==B){//true System.out.println("Equal"); } if(A.equals(B)){//true System.out.println("Equal"); }
Ok if you use
int[] B = A;
then Bthey Arelate to the same object, so they are trivially equal. The first comparison (==) will definitely return false between Aand A.clone(), since the values refer to different objects. It seems that arrays are not overriding equals(for example, how ArrayList), so the clone is not equal to the original in the method equals.
A
A.clone()
equals
ArrayList
EDIT: , 10.7, :
, Object; Object, , clone.
, clone(), toString/hashCode/equals.
clone()
toString
hashCode
-, equals Java ( , ==).
==
, - . , , .
Java
java.util.Arrays.equals(a,b);
a == b, - .
a == b
a.equals(b), , , Object, ==.
a.equals(b)
, , , Arrays.equals() . a.equals(b), ... .
Javadoc clone():
http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#clone%28%29
:
[ clone()] . "" . , x, : x.clone() != x : x.clone().getClass() == x.getClass() , . , : x.clone().equals(x) , .
[ clone()] . "" . , x, :
x.clone() != x
x.clone().getClass() == x.getClass()
, . , :
x.clone().equals(x)
, .
B , A, , .
when you assign B = A, you assign a reference to the same object. Using clone () you get a copy of the object. The equality operator (==) checks whether both characters refer to the same object where the .equals method checks whether both objects have the same value (defined by the class implementation)
Source: https://habr.com/ru/post/1794181/More articles:Javascript /// For instances of custom objects - javascriptSet up operations in the Appengine data warehouse - google-app-engineASP.NET Desktop - concurrencyHow to unit test my Google OpenID consumer app? - unit-testingMultiple property files - javaMultiple .properties files in a Java project - javaPHP SoapClient Creating a Different Formatted SOAP Request - soapNhibernate filter does not apply sequentially to child collections - .netЕсть ли хорошее решение для поиска в TabActivity для каждой активности на вкладке соответственно? - androidMarshallin object with StringBuffer attributes - javaAll Articles