Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Headfirst Java sorting page 566

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help look at the pictures the highlighted part. I know the first part is the reference but whats the other bit is confusing "(boo) b". can somebody please tell me whats going on there so confusing the more detail the better
page-566-headfirst.PNG
[Thumbnail for page-566-headfirst.PNG]
 
Marshal
Posts: 74780
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is old‑fashioned code because it doesn't use generics. What you have to do it assume that the Object parameter passed is in fact a Book, and then cast it to a Book object, not boo. What you are telling the javac tool is, “Don't worry; I know it will be a Book.”
You would write something different nowadays:-Now, there is no risk of passing the wrong type; the compiler will notice and not permit the wrong type.
Note that the title given here is a String and String already implements Comparable<String>, so you can implement that method quite simply by borrowing the String version.
 
Bartender
Posts: 4752
184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Elim,

as you can see, the parameter is 'Object b' in the compareTo method. Now, an Object does not have a field called 'title'. So, assuming that b is actually a Book, we must cast b to a Book with:

and now we can use book.title.

Mind you, this is old non--generic code. If b is not a (subclass of a) Book, then this code will raise a ClassCastException.

I cannot see what the book is telling about the generic version of Comparable, but nowadays you would use:

and the compareTo method would have a Book b as argument, so there is no need to do a cast anymore.
 
Ranch Hand
Posts: 276
2
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, please don't forget to upgrade your JDK version to greater than 1.5 as generics came into picture from Java 5.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic