This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics are going to be the death of me.

 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic