O'Reilly Forums: Array Vs. Collection Type Checks, Pg. 572 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Array Vs. Collection Type Checks, Pg. 572 comment: compiler accepts potentially ok situation

#1 User is offline   FoundIt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 15-January 13

Posted 15 January 2013 - 12:51 PM

I wonder if this comment on page 572 could be improved:

"We put a new Cat into a Dog array. The compiler allowed it because it knows that you might have passed a Cat array or Animal array to the method, so to the compiler it was possible that this was OK"

The example on the page before (pg. 571) mentions how the compiler just won't let you take the risk of passing a Dog ArrayList to a method that takes an Animal ArrayList argument because someone might try to, say, add a new Cat object to that ArrayList.

The comment on 572 seems to justify the compiler's permission (the operation may work sometimes) for the exact same reason it's denied in the ArrayList scenario.

I understand that main point is that the difference is because "Array types are checked again at runtime, but collection type checks happen only when you compile" but maybe a postscript to the comment could be "It's only because of that second check at runtime that the compiler can afford to be uncharacteristically relaxed and open a beer at the party."
:)

It's a small point, but I like the book and think it should be even better, if possible.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users