• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Ridiculous use of generics in K&B page 626

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, K& B Page 626, I see this sentence "This is a ridiculous use of generics and in fact you will see generics only rarely outside of collection". What does it mean ?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that I do not know the full context, I guess it means generics is being used generously in this class.

Outside of Collections, generics is indeed used sparingly.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Book doesn't provide any more information other than given below.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:Book doesn't provide any more information other than given below.



As Deepak said, Generics is mostly seen with the Collections. As far as I have seen, Generics is not used much elsewhere.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, What is the culprit in this code ? It looks & compiles fine
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:So, What is the culprit in this code ? It looks & compiles fine



Is there any? I dont think so.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"?" mark is not mandatory.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:So, What is the culprit in this code ? It looks & compiles fine


SCJP is full of examples that compile and run fine, but that would never be written that way in real life. There are lots of places you can use generics, but that doesn't mean it's worth the effort!

In this case, you're basically creating a class that can wrap any kind of object. But usually you could just use the object.

Collections are one place where they are really useful because you keep wanting to use the same sort of data stuctures (lists, maps etc) on different kinds of object.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mat, I am still not clear. Here is the code that I can call to the TestGenericClass class. This looks good to me. If not, Could you please point out where I am going wrong ?

Matthew Brown wrote:
In this case, you're basically creating a class that can wrap any kind of object. But usually you could just use the object..

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:Hi Mat, I am still not clear. Here is the code that I can call to the TestGenericClass class. This looks good to me. If not, Could you please point out where I am going wrong ?


I don't think you're doing anything wrong. It's just an example to show how generics work. What K&B mean is that there would be little point doing this in a real project.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B clearly mentioned "This is a ridiculous use of generics" means BAD..What makes it to be bad so that I can avoid using that part.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid it by using generics only when there's a good reason to. In the example they've given...why would you do that? What problem would you try and solve in that way? All they mean by it (I think) is that they've made up a very artificial example that is designed to illustrate how generics work rather than to solve a problem.

Don't worry so much about it!
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code looks fine to me. I don't understand why it is bad as stated in the book. Maybe, somebody will let us know

Saibabaa Pragada wrote:K&B clearly mentioned "This is a ridiculous use of generics" means BAD..What makes it to be bad so that I can avoid using that part.

 
Sheriff
Posts: 24635
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't look especially ridiculous to me. However since it's a typical SCJP example with no relationship whatsoever to the real world, you wouldn't expect it to be especially meaningful anyway. And it isn't. It's rather pointless. But I wouldn't go so far as to call it "ridiculous".
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ridiculous" is used in K&B book. Wondering, which statement is bad
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:"ridiculous" is used in K&B book. Wondering, which statement is bad



None of them are.
 
Nothing? Or something? Like this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!