aspose file tools*
The moose likes Beginning Java and the fly likes Generics are going to be the death of me. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics are going to be the death of me." Watch "Generics are going to be the death of me." New topic
Author

Generics are going to be the death of me.

Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Ok, i'm having a bit of a problem with the following code. I think there is just something I keep missing here.

Given:


Regardless of the result I'm missing out on the understanding of the following code.



Ok, if <V,K> is the return type and <K,V> is the type being passed in. What is the "public static <K, V>" defining?


Rob Mech, SCJP 1.5<br /><a href="http://www.robsprogrammingjunk.com/" target="_blank" rel="nofollow">http://www.robsprogrammingjunk.com/</a>
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Rob Mech:
Ok, i'm having a bit of a problem with the following code. I think there is just something I keep missing here.

Given:


Regardless of the result I'm missing out on the understanding of the following code.



Ok, if <V,K> is the return type and <K,V> is the type being passed in. What is the "public static <K, V>" defining?


That is simply saying that you are going to have two type variables, K and V.
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Originally posted by Keith Lynn:


That is simply saying that you are going to have two type variables, K and V.


Two type variables where? The input parameter and the return type are defined. I guess that's what I'm missing here.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
But you're input parameter and return type are parameterized. They expect two type variables K and V.

Note what happens when you try to compile this.



You get the error



You have to specify that you are going to use two type parameters.

So you would need to add <K,V> or <V,K> before the return type.


[ April 24, 2007: Message edited by: Keith Lynn ]
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1344
    
  12
Hi Rob,

When the documentation says <K, V> it is talking about a Key object and a Value object.

An example would be:



As you can see, a very contrived example, but hopefully it describes the use of <K, V>.

The key and value must both be Objects - so no primitives. If you want to add an int, it is going to have to be wrapped as an Integer.

Keep asking!

Katrina
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Type definition got it. So what we're saying is that we're going to use those two letters. Got it.

One more question then. Does the order matter at all or is it the equiv of saying

<K,V>
or
<V,K>

While it probably makes sense to define them in the same order, does it have any bearing as to the operation itself?
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Originally posted by Katrina Owen:

The key and value must both be Objects - so no primitives. If you want to add an int, it is going to have to be wrapped as an Integer.

Katrina


According to the solution to the original question at hand, all that results is a compiler warning. I havent actually tested the code myself but based on what else I reviewed wouldnt the primitived be autoboxed? It doesnt have to be widened, just boxed so wouldnt that operation take place automatically. I understand WidenAndBox is not allowed, but Boxing should occur.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In this case, the order doesn't matter.

However, it is possible to use type variables in their own bounds and in the bounds of other type variables.

You can't mention a type variable that appears to the right of the one you're defining.

That is, you can't do this.

Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1344
    
  12
You would have to define the map as



at which point autoboxing would take place when you added something like this:



however, you couldn't define the map using
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Originally posted by Katrina Owen:

at which point autoboxing would take place when you added something like this:




Right, so in the very first example in this thread, the autoboxing does occur.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1344
    
  12
Yes, autoboxing does occur in the first example.

Hope I haven't confused things! (I'm pretty new at this, and try my hand at answering when I think I might help. Sometimes it doesn't work so well )
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
Originally posted by Katrina Owen:
Yes, autoboxing does occur in the first example.

Hope I haven't confused things! (I'm pretty new at this, and try my hand at answering when I think I might help. Sometimes it doesn't work so well )


Nope, not confused at all. I have it all straight now. Thanks everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics are going to be the death of me.
 
Similar Threads
Generics and Colletions
MasterExam question
Generics doubt-How do I read this
Help understanding a generics and collections question
more generics