• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in book on "Be the Compiler"?  RSS feed

 
Jacob Collins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Head First Java, there is a section of "Be the compiler, advanced" in the chapter called "collections with generics."

The answers the book gives do NOT agree with my compiler. Or even common sense in some case. The relevant pages are 576 and 579.

The problems are based on the following classes:
abstract class Animal {
// whatever here
}
class Dog extends Animal {
// whatever here
}
class Cat extends Animal {
// whatever here
}

The books says these lines should compile, and my compiler won't accept either one:
ArrayList<Dog> dogs1 = new ArrayList<Animal>();
ArrayList<Animal> animals1 = new ArrayList<Dog>();

Are these supposed to compile?

The book also said that the following won't compile, and yet it does for me:
ArrayList<Object> objects = new ArrayList<Object> ();

Even though it compiles for me, should it not compile for others?

I'm just really puzzled because these aren't listed as errata anywhere so I'm wondering if I have something wrong.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I can't find your earlier post where you mentioned this problem. Please check it again carefully.

1: If that doesn't sort it out, make sure you have the whole code copied exactly, to the very letter, including capitals, from the book.
2: Make sure you have understood the question exactly, and whether the X means it works or doesn't work.
3: Check the publication date of the book and which version of Java it works on. From the description you give, it ought to be 5 (J2SE5.0). Make sure you have the same version. From the description you give, you probably have 5 as well. J2SE5.0 came out about September or October 2004, and has to date 7 updates. But the updates oughtn't to make that sort of difference; they are minor bug fixes.
4: Copy the entire exercise again starting from scratch and see what happens.
5: Compare what you have with the downloaded version of the exercise; most books have an associated website with the exercise on.

ArrayList<Dog> foo = new ArrayList<Animal>(); ought NOT to compile.
ArrayList<Animal> bar = new ArrayList<Dog>(); I think that will compile with a warning.
ArrayList<Object> oof = new ArrayList<Object>(); ought to compile.

If after all this lot, and checking your original post, you might have found an error in the book, but I would have thought that it would have been reported in the 18 months the book has been available. But make sure to check very very carefully before reporting it to the publishers.

CR
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, it wasn't your previous post. It was this thread.

CR
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More about compiler messages. Be very careful about messages from javac: they sometimes appear after the real error.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at the book, and it appears the Xs are meant to indicate which answers are correct.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody else has already had the same problem. Look here.
Do you chaps know each other?

I have managed to find a copy of HFJ, and looked at the exercise in question, and I copied and pasted what you two quoted into Eclipse, and got all sorts of compiler errors. When I sorted out the imports, and //commented out the lines which still had red marks against them, I ended up with 34678, so I don't think there is an error in the book at all. Keith Lynn is right, the X means it will compile.
[ June 15, 2006: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I said earlier
ArrayList<Animal> bar = new ArrayList<Dog>(); I think that will compile with a warning.
I was mistaken. It won't compile, and the HFJ exercise explains why not.
 
dave hopkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh my god! I've just spent the last day trawling the Internet trying to find out why the examples on page 579 with X's next to them won't compile and all the other ones will. Now I discover I'm not losing my mind and in fact a 'cross' in this case really means a 'tick'! Don't know whether to laugh or cry!
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dave hopkins:
Oh my god! I've just spent the last day trawling the Internet trying to find out why the examples on page 579 with X's next to them won't compile and all the other ones will. Now I discover I'm not losing my mind and in fact a 'cross' in this case really means a 'tick'! Don't know whether to laugh or cry!


Evidently you chose option C) register just to resurrect a dead thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!