• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

java generics behaviour

 
Ranch Hand
Posts: 30
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java generics are resolved during compile time and also primitive operations on types must be resolved during compilation then why cannot we specify some operation on java generics?
I mean something like

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by primitive operations being resolved at runtime, but the compiler does know what operations are valid, given the types used. In a similar way, the compiler will only allow operations on generic types that it can guarantee exist. How does it know that + will be valid in that example?
 
Gautam Bhalla
Ranch Hand
Posts: 30
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hye,got your answer thanks.then what was exact use of generics apart from collections.


 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anywhere where you have a class that can be used "generically" with different classes while still wanting to have type safety. Collections are definitely the most common use, though.

If you want another non-collection example from the Java libraries, have a look at java.util.concurrent.Future. That's used for asynchronous calls, where at some point in the future you'll be able to get a value back. The generic parameter allows this value to be of any specific type, so you still get type safety without having to cast it.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This discussions makes me paste a small piece of code which I had written the other day;


In the above code I have defined an interface with a generic parameter. The same has been implemented by different operations each specifying its type data.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gajendra Kangokar,
Your post was moved to a new topic.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you're looking at this with a C++ background (thinking about C++ templates).

There's a big difference between the way templates in C++ and generics in Java. In C++, a template is really a thing that generates code. So if you define a function like this:

And you use that somewhere on two integers, then the compiler will generate a new add() function from the template that works with integers. In other words, it automatically generates and compiles a function that looks like this:

That's not how Java generics work. The Java compiler does not generate variants for the specific types you are using. Instead, it creates just one version of the method, in which the generics are removed. So the Java compiler would try to make something like this out of your generic method:

That's obviously not going to work, because you cannot add two Objects with the + operator.
 
You got style baby! More than this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic