Up vote 1 down vote favorite 3 share g+ share fb share tw.
Having the below code: Stack integers = new Stack(); Stack numbers = integers; Number n = numbers.pop(); numbers. Push(3); numbers. Push(n); I get compilation errors on the last two lines, but although I've given it some thought I don't understand why the compilation errors are there.
The method push(capture#2-of? Extends Number) in the type Stack is not applicable for the arguments (int) When I comment the last line, I still get the above compilation error, but from my understanding the compiler should be able to infer the correct type (Stack) from those lines. Thanks a lot java generics bounded-wildcard link|improve this question edited Mar 30 at 2:37Jeff Axelrod2,295733 asked Jun 25 '11 at 7:45yas48911,8411520 96% accept rate.
Please check the other questions at SO, there are lots of similar questions to this one. – Augusto Jun 25 '11 at 7:52 I've searched through 5-10 of them and they seem to be about how to cast List to List and similar. I'll go and have a closer look though – yas4891 Jun 25 '11 at 7:58.
Thanks a lot for this link. – yas4891 Jun 25 '11 at 8:03 well sometimes is easier to redirect to an already existing well documented response. Please give credit to that response :) – Cris Jun 25 '11 at 8:08 alos guys from here that spend their time to help you more then me...deserve that :) – Cris Jun 25 '11 at 8:09.
The last two lines aren't valid because numbers could be a stack of any numeric type. Consider this similar code: Stack doubles = new Stack(); Stack numbers = doubles; Number n = numbers.pop(); numbers. Push(3); numbers.
Push(n); Here you're trying to put an Integer onto a Stack which obviously isn't correct. Basically when you use wildcarding like this, you can get values out, but you can't put values in because the compiler can't guarantee it's valid to do so.
From reading this and the link provided by @Cris I seem to understand the problem at hand. However, why can't the compiler infer the type, when there is only one element (of type Integer) pushed onto the stack? – yas4891 Jun 25 '11 at 8:04 @yas4891: The compiler isn't meant to infer anything from the previous lines of code - only from the type of the variable, which is Stack.
– Jon Skeet Jun 25 '11 at 8:15.
Generics covariance in Java is handled at the client. I.e. You don't have the semantics to say that a Stack is covariant and let the compiler check that the operations you are allowing (like push) is valid in a covariance model.
(push is not). The specific issue here is that you can do something like this: Number r = new Rational(a,b); // rationals are also numbers number. Push(r); which in the underlying structure implies integer.
Push(r); // type mismatch (Programming in Scala has a crystal clear explanation of client vs provider side (co-/contra-)variance on Chapter 19. A recommended read even if you are not into Scala).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.