Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

equals in the case of arrays  RSS feed

 
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These questions are taken from SCJP 5 quiz by Mark Dechamps (Inquisition). I am unable to understand the answers. Can someone help me?

What prints on screen?

Answer is true

-------------------------------------------------------------
Consider this piece of code, what comes on screen when this runs?


Answer is false
[ September 29, 2008: Message edited by: Vyas Sanzgiri ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first one, here is the method explanation from Java API doc.

public static boolean equals(int[] a, int[] a2)Returns true if the two specified arrays of ints are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order


As per the above, it returns true.

The second one, I am also confused as you are.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vyas,
For the second one, here is the explanation:

Calling equals() method on an array is the same as using the == operator, because arrays always inherit the default equals() method that compares references rather than array contents. So, you get false.

ie. Original Object's equals() method compares references and integer array objects (all primitives, I believe) do not override the equals() method of Object. Whereas, String class overrides to return true if the contents of two Strings are the same.

Anyone, please correct me if I am wrong.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody's reply on this???
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rekha,
Your second explanation is also absoultely correct. In the second case, a and b both are arrays, so both are objects. Since the default implementaion of equals() merely does a reference check (if a==b), it returns false.

Both these questions are from John Meyer's exams. I just love those questions, they make you think

Cheers!
Seema
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, agree. This question was very interesting, and clarified a
probably-missed-out-part of equals() and ==
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Here is an example to illustrate ==, equals, Arrays.equals

import java.util.Arrays;


public class MyApp {
public static void main(String[] args) {

// No matter how you define, arr1, arr2, arr3 and arr4 are four differnt object
int [] arr1 = {1};
int [] arr2 = new int[] {1};
Integer [] arr3 = {1};
Integer [] arr4 = new Integer[] {1};

// As these are four differnt objects, == would return false
System.out.println("arr1 == arr2 = " + (arr1 == arr2));
System.out.println("arr3 == arr4 = " + (arr3 == arr4));
// Folllowing code won't complile as arr2 is int[] and arr3 is Integer[]
//System.out.println(arr2 == arr3);

// As Array hasn't overriden equals, so it inherit from java.lang.Object
// which is same as ==
// so follwoing would print false
System.out.println("arr1.equals(arr2) = " + (arr1.equals(arr2)));
System.out.println("arr3.equals(arr4) = " + (arr3.equals(arr4)));
System.out.println("arr2.equals(arr3) = " + (arr2.equals(arr3)));

// We can use Arrays.equals method to check equality
System.out.println("Arrays.equals(arr1, arr2) = " + Arrays.equals(arr1, arr2));
System.out.println("Arrays.equals(arr3, arr4) = " + Arrays.equals(arr3, arr4));
}
}
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes Rekha, John Meyer's exam has been a real eye opener
It was good fun
seema
 
Mohammad Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


// As Array hasn't overriden equals, so it inherit from java.lang.Object
// which is same as ==



I shouldn't say equals method is same as ==
rather, if equals not overridden, it return true if == return true otherwise false
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!