-1

I am reading Oracle Java Certification book. I'm finding following sentences hard to grasp.

Two arrays with the same content are not equal but ArrayLists equal.

And If you call remove(0) using an empty ArrayList object, it will compile successfully :/

What does it mean ? Wonder why and how ? Can anyone explain it ?

Resul Rzaeeff
  • 448
  • 4
  • 14
  • 30
  • Define *equal* - are you referring to invoking the `equals` method? Why should it not compile?? – luk2302 May 25 '17 at 17:56
  • I am reading OCA Java 8 book . These sentences written in there. But it is a little bit confusing.. Yeah refering invoking method – Resul Rzaeeff May 25 '17 at 17:59

1 Answers1

3
  1. because equals on arrays test for reference equality, wether or not the two variables are actually references to the same object. See https://stackoverflow.com/a/8777279/2442804 ArrayList implements it differently and checks its contents
  2. Why should it not? It is just some method call one some object that is not allowed at runtime. But that is nothing the compiler is supposed to check - period. The call compiles fine, but at runtime the code detects that you are not allowed to call that method in the given situation.
luk2302
  • 55,258
  • 23
  • 97
  • 137