Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Doubt" Watch "Generics Doubt" New topic
Author

Generics Doubt

Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Hello ranchers

Consider this code snippet:

import java.util.*;



At line 2 I am getting a compiler error.

But If I change line 1 to
List <? super Integer> al = new ArrayList<Integer>();
It doesnt give me any error.

What is the difference in method call between the two code.

And what will be parameterized type 'T' here. Is T -> ? extends Integer or is T -> Integer.

Thanks
Srividhya
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am not sure why you are getting Error in extends and not in Super,
Because as far as I know,
Both the type shouild be exactly the same.
Both sides should have <Integer> and either one should not be a super or subclass.

May be some one can help you out and give you the precise reason.
Good Luck.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Joe Fischer
Greenhorn

Joined: May 02, 2008
Posts: 25
Let's say that Short extends Integer (which, of course, it does not). Then you could say:

So you're calling the go() method with T of Integer meaning that the method is set up to deal with Integer types. The List parameter is constrained to Short which is more restrictive than Integer. It like down-casting a type to a sub-type, the compiler will allow it but the JVM will throw a ClassCastException. Any super-type of Integer is safe to use for the List, therefore the compiler allows the "<? super Integer>" construct.


SCJP-SE6.0
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Dear Joe, I didnot understand your point. Can some one throw more light on this.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
You should read the following link for more understanding

Generics Wildcards

In short with extends you define a list with a upper bound and you don't know which subtype you will add. So the objects you add could not be added in your defined collection.

With super you define a lower bound of your collection and every object you add could be casted to the type of your collection.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

I understood your explnation, but have a look at below code



I am getting compile time error at line1,but for me assignment and method passing looks pretty same,the only difference i can notice is method call is is a generic method.

I guess Line1 is not compiling because a1 can take any ArrayList which is super type to Integer,that is why we can not assign it to Integer list,but how it is working for method call
[ May 18, 2008: Message edited by: ramesh maredu ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

I guess Line1 is not compiling because a1 can take any ArrayList which is super type to Integer,that is why we can not assign it to Integer list,but how it is working for method call


You are assuming that with the method call, that T is an Integer. It isn't. In this example, T is "? super Integer" -- which fits both the Integer type and the generic of List type.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

Thanks for your reply, i did some search and found that

"The compiler infers the type argument for us, based on the types of the actual arguments. It will generally infer the most specific type argument that will make the call type-correct."
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Dear ramesh, can you please explain what that means?
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Hello All

Thanks for all your reply. Still I am not getting what will be the 'T' in case of
List<? super Integer> l=new ArrayList<Integer>();
go(l,new Integer(10));

Is T <? super Integer> or Integer or Object since it is the super type of all class.

Srividhya
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

I found following link in sun website
Generic Methods

according to above link i guess it will consider T as (? super Integer),correct me if i am wrong

[ May 19, 2008: Message edited by: ramesh maredu ]
[ May 19, 2008: Message edited by: ramesh maredu ]
ramchander yshetti
Ranch Hand

Joined: Apr 22, 2006
Posts: 97
hai Srividhya Kiran

import java.util.*;
class Demo{
public static void main(String args[])
{
List <? super Integer> al = new ArrayList<Integer>();//line 1
go(al,new Integer(10));//line 2
}
static <T> void go(List<T> l,T o)
{
}
}
here T is Integer only

to be more clear

watch this

abstract class Animal
{ ....}
class Dog extends Animal{ //implementing....}
class cat extends Animal{//.................}
class Bird extends Animal{......... }


List<? super Dog> list=new ArrayList<Animal>();

here <? super Dog>

we can pass a Dog Object or
Animal object because Animal is the super class
where it can take <? super Dog>

and we cannot pass the Cat object.

if we want to pass the Cat object then <? super Cat>

----------------
abstract class Number
{........}
class Integer extends Number{.......}
class Short extends Number{.......}
class Long extends Number{........}

----------------see this code it will compile

import java.util.*;
class Demo12{
public static void main(String args[])
{
List <? super Integer> al = new ArrayList<Number>();//line 1
go(al,new Integer(10));//line 2
}
static <T> void go(List<T> l,T o)
{
}
}
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

even thogh this thread is old i guess now i can give proper explanation that is why i am posting my reply

In below code i wrote, i have a static method test which takes two argumets x and y of generic type T.

when i invoke the this test method with parameters Integer, integer then compiler assumes T as Integer no confusion in it.

But when i invoke the same method with Integer,Float parameters,java compiler is smart enough to identify T in the way explained below.

"Compiler in this case looks for the common super type of both arguments it can be class or interface so the common class for Integer and Float is Number so it can assume T as Number.And in case of interface it is Comparable common for both so it can assume T as Comparable".


Code is given below.




[B]Still I am not getting what will be the 'T' in case of


[/B]
Here one argument is ? extends Super and another is Integer so compiler can assume T as Object only as it has no other choice, since ? super Integer can be any super type of Integer.

see the code below


[ May 24, 2008: Message edited by: ramesh maredu ]
Daniel Del Moral
Ranch Hand

Joined: May 24, 2008
Posts: 32
About the original post. You can't pass a variable declared with generic type <? extends AnyClass> to any other method. This is for safety, generics types that extends can't be added any item.

Inside the same method, the compiler can monitor that you don't call the add method of the array, but because it can't monitor you can't do it outside, you simple won't find a way to call a method using that variable.
[ May 24, 2008: Message edited by: Daniel Del Moral ]

SCJP 5, SCWCD 5
kapil kumar
Greenhorn

Joined: May 22, 2008
Posts: 25
Hi ramesh ...........
Can you please elaborate on your recent post/


If you can't be the Sun ..<br /> Be a star ..............
 
Don't get me started about those stupid light bulbs.
 
subject: Generics Doubt