• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics Doubt

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Joe, I didnot understand your point. Can some one throw more light on this.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 23951
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

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
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear ramesh, can you please explain what that means?
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ramesh ...........
Can you please elaborate on your recent post/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic