• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

OCA, Chapter 3, Question 19 - equality confusion

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the book, the statement "Two arrays with the same content are equal" is considered wrong (question in page 158, answer in page 341), however java implements equals methods for arrays (https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html) which compare each element separately and if all pairs are same then the arrays have same content and are equal.

Is this an errata?
 
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the arrays have the same size? An array is created with a constant size. The following two arrays could not be equal since they differ in size. When the docs refer to the number of elements they really mean the size of the array. So the following would not be equal.
int a[4] = {0, 1};
int b[5] = {0, 1};
 
Marshal
Posts: 69740
277
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William Ng wrote:. . .
int a[4] = {0, 1};
int b[5] = {0, 1};

That doesn't look like Java® code to me; I don't think it will compile. You cannot specify the size of an array in its declaration; that looks like C code where arrays are completely different kinds of data structures from Java®. Anyway, you are specifying 4/5 elements as the size and then assigning the array to two elements. That will present problems in C, too.

Ioannis Gkinalas wrote:. . . implements equals methods for arrays. . . .

If you look in the Java® Language Specification (=JLS), you will find that arrays inherit all methods of Object unchanged except clone() which is overridden. The arrays will therefore use the unoverridden equals() method which is equivalent to using the == operator. That is why the Arrays class supplies a different equals(...) method, whose details are available in that link.
We would need more details of what the question says before we can understand why you think the answer given is wrong.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic