aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Basic doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Basic doubt" Watch "Generics Basic doubt" New topic
Author

Generics Basic doubt

srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
hi i didnt get this point from java beat mock exam.

class GenTest<T super Number> {

T num;

public T checkNumber(T n) {

return n;

}

}

compilation fails as <T super Number> is invalid syntax
please throw some light on this concept.


SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Hi,

The problem is when you say that your type has to be a super type of Number.
If you type the class as extends from the Number...everything goes fine.

What can be a super type of Number that you want as a class type?

There lies the problem.
Thanks.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Java does not allow lower bound constraints on type parameters, so "<T super Foo>" is always invalid syntax. Lower bounds are only allowed for wildcards, e.g. "<? super Foo>". This limitation was imposed because of the way Java's type inference system works.


SCJP 5.0
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Hi Kelvin,

Can you explain a bit more with code.
I got your point but no idea why java's type inference system doesnt allow this.

Thanks.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
The details are highly technical, and honestly I don't really understand it all either--it's been too many years since my last programming language theory course in college!

But if you're interested, check out this page in Sun's bug database, which contains this explanation from Sun:

See
http://lampwww.epfl.ch/~odersky/ftp/local-ti.ps

particularly section 3 and the last paragraph on page 9. Admitting
type variables on both sides of subtype constraints can result in a
set of type equations with no single best solution; consequently,
type inference cannot be done using any of the existing standard
algorithms. That is why type variables have only "extends" bounds.

Wildcards, on the other hand, do not have to be inferred, so there
is no need for this constraint.


The provided link is for "Inferred Type Instantiation Without Prototypes for GJ" (Postscript format), a paper on Generic Java (the predecessor of Java 1.5 generics). Warning: if you don't have a Computer Science background, much of the notation in that paper will probably look like gibberish to you.

I haven't had time to really make sense of the above information yet, so if someone else can translate it into a more layman-friendly description, that'd be most appreciated.
[ December 10, 2007: Message edited by: Kelvin Lim ]
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
HI KELVIN,

THANKS A LOT.

AM NOT A COMPUTER SCIENCE STUDENT BUT STILL WILL GIVE IT A TRY.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Basic doubt