Lower bounds

Using generics for the first time I stumbled upon this basic situation:
ArrayList<Number> arrayList = new ArrayList<Integer>(); //compile error
Number number = arrayList.get(0);...then I found the correct syntax to make it work:
ArrayList<? extends Number> arrayList = new ArrayList<Integer>(); //correct
Number number = arrayList.get(0);...that because there is a lower bound feature and the "extends" and "super"keyworks is needed to differentiate between them:
ArrayList<? super Integer> arrayList = new ArrayList<Number>();
Object number = arrayList.get(0); //returns java.lang.Object...which has the same effect as:
ArrayList arrayList = new ArrayList();
Object number = arrayList.get(0);...which adds nothing compared to not using generics and making lower bouds almost useless.
Of course lower bounds add an extra check and this code doesn't compile:
ArrayList<? super Number> arrayList = new ArrayList<Integer>();
Object number = arrayList.get(0);...but does this kind of check has really any use?!
Its like wanting the following code to not compile:
Number number = new Integer(0);...but why would anyone want that?
Defining upper bounds is necessary to check types at compile-time and avoid NoSuchMethodError to be thrown, but I found no use to lower bounds besides hampering polymorphism and making the more useful upper bound syntax more polluted.
Maybe I didn't get the idea behind lower bounds... can anyone show me some real use to it?

...when you have 2 different generic classes and need to exchange data between then you must have a common upper and lower boundary. Got the idea... thanks.

Similar Messages

  • Javac bug with lower bounded wildcard?

    Hi all,
    I have found the below code snippet not to compile with javac (1.5 & 1.6) even though
    I think that it is correct. Also the Eclipse Java compiler accepts this code without errors.
    Can anyone confirm that this is a bug in javac or maybe explain why the code is really wrong as javac is claiming?
    Thanks a lot for any help!
    $$$ cat A.java
    interface I { }
    class C1 { }
    class C2 extends C1 implements I { }
    public class A<T extends C1 & I> {
    A<? super C2> a;
    $$$ javac A.java
    A.java:6: type parameter ? super C2 is not within its bound
    A<? super C2> a;
    ^
    1 error

    Hi Danny,
    thanks for your feedback. Actually, "? super C2" does not allow any arbitrary supertype of C2 since each type argument must also respect the bounds of the type parameter. Therefore, "? super C2" represents all supertypes of C2 which are also subtypes of C1 and I. In our case here, the only such type is C2.
    Here is also an attempt at showing that the code is correct based on the JLS3: Any type parameter is correct iff it is a subtype of all the types in the bound of the type parameter. To check this, you should take the capture conversion (JLS3 5.1.10) of "? super C2" which is a fresh type variable, let's say CAP, with the upper bound "C1 & I" which clearly is a subtype of the type parameter T as it has the same types in the bound. Or am I just missing something?

  • Disbaling No upper/lower bound value in a date range parameter

    Hi folks,
    When i added a new parameter with a date range , i can see two check boxes 'No lower value' and 'No upper value' along with 'include value' checkbox in the parameter dialog.
    Is there any way to disable these check boxes?? If i cannot disable check boxes and when i select no lower value check box some random date value get returns to the query and the SQL query is giving no results and also i cannot set to a default lower and upper date range too.
    Please help me in resolving this.
    Thanks in advance.

    These features are respective to the range component. If you do not need them, I would suggest that you use two date parameters. One date parameter can act as your low date parameter, and can have it's own default date. The other parameter can act as your high date parameter, and can have it's own default value.
    The only change essentially, in the way the report runs, should be some minor changes to formulas, for example instead of having a selection criteria formula that says in {?DateRange}, you would now say >= {?StartDate} and <=
    Edited by: Kyle McAdam on Aug 15, 2008 6:31 PM

  • Explanation on Greatest Lower Bound and Least Upper Bound.

    Hi All,
    Can any one explain how Oracle Implements GLB and LUB with examples.
    Have a Nice Day.
    Thanks & Regards,
    Ahmed.

    Doc is here :
    http://download.oracle.com/docs/cd/B19306_01/network.102/b14267/worklabel.htm#i1008754
    Nicolas.

  • Setting the max and min bounds of a result set

    I'm trying to set the upper and lower bound values of a result set. Meaning, I want to limit the values in my result set to not be below/above a certain number, BUT I want any number in my result set that is below/above the lower/upper bound that i have designated to be decoded to the lower/upper bound number that I have designated. Here's an example to help clarify:
    WITH temp_table AS
    SELECT 1231.12 AS col FROM dual UNION ALL
    SELECT 1001.00 FROM dual UNION ALL
    SELECT -32.0 FROM dual UNION ALL
    SELECT -3.0 FROM dual UNION ALL
    SELECT 332.0 FROM dual UNION ALL
    SELECT 211.0 FROM dual
    SELECT CASE WHEN col < 0 -- 0 is the lower bound
                THEN
                     0
                WHEN col > 1000 -- 1000 is the upper bound
                THEN
                     1000
                ELSE
                     col
           END as desired_result
    FROM   temp_tabledesired result set:
            DESIRED_RESULT
         1000
         1000     
            0
         0
         332
         211I was wondering if there was another function or method of achieving my desired result set without having to use a CASE statement? I have to do this to several columns, and it doesn't seem very efficient to throw a CASE statement around each one.

    LEAST and GREATEST will do that too :
    SQL> WITH temp_table AS
      2  (
      3   SELECT 1231.12 AS col FROM dual UNION ALL
      4   SELECT 1001.00 FROM dual UNION ALL
      5   SELECT -32.0 FROM dual UNION ALL
      6   SELECT -3.0 FROM dual UNION ALL
      7   SELECT 332.0 FROM dual UNION ALL
      8   SELECT 211.0 FROM dual
      9   )
    10  SELECT least(greatest(col, 0), 1000)
    11  FROM temp_table
    12  ;
    LEAST(GREATEST(COL,0),1000)
                           1000
                           1000
                              0
                              0
                            332
                            211
    6 rows selected
    I have to do this to several columns, and it doesn't seem very efficient to throw a CASE statement around each one.Any test case showing CASE as not efficient?
    Edited by: odie_63 on 25 juil. 2012 23:20

  • Do wildcards have to be within bounds?

    Is it intended that the compiler accepts the following type declaration?
    class Wrapped<T extends Comparable> { ... }
    Wrapped<? super Number> wrapper3;  // <<<Number is not Comparable, and for this reason no supertype of Number is Comparable. Consequently the set of types denoted by Wrapped<? super Number> is empty. Why does the compiler allow nonsensical type declarations?
    The specification does not say anything about the lower bound of a wildcard, but I had expected that of course the lower bound of a wildcard is checked against the upper bound of the type parameter the wildcard stands for. No? Wrong idea?

    We've flipflopped on this issue a couple of times. At first we though we should check the bounds, but that isn't right, because then you can't write (ignoring the fact that Comparable is generic)
    Wrapped<? extends Number>
    This is perfectly harmless, and denotes a nontrivial and useful type. So we dropped the checking altogether. That is too relaxed, as you can see. Our current plan, due to Christian Plesner Hansen is:
    given this declaration "class C<T extends X>",
    * C<Y> is legal if Y <: X (the current rule)
    * C<? extends Y> is legal if Y is castable to X
    * C<? super Y> is legal unless Y cannot possibly be a subtype of X
    The prototype compiler doesn't currently have this rule nor "castable" implemented, nor are they specified in the jsr14 spec that I distributed. I'm working on this now, based on very nice code supplied by Christian, but I'm afraid I may not have it in the prototype until October.

  • Is lower than 64 kbps MP3 format any good?

    I've converted all my iTunes Library to MP3 - 64kbps format. I am very pleased with the quality but was wondering if I go any lower will it sound much worse?
    I've got nearly 2,000 songs on my iPod and growing. That's why I've converted my songs to such a low bitrate.
    Thanks in advance

    For normal music listening, most people view 128 as the lower bound for acceptable audio.  This of course will depend on your ears, your equipment, and the noise level of your surroundings.
    For spoken word recordings, 64 or 48 would be fine.
    Do not  get into the trap of butchering your music to fit your current iPod.  Use the iPod ability to "sync selected playlists" to put a selection that will fit on the iPod.

  • Bounds are not propagated in extended class

    Have a look on this small example, 3 classes A, B and C.
    B and A are generics class,
    B herits of A and there is a lower bounds on the class T in B (T extends C)
    public class A<T> {
         public A() {}
         public T foo()
    public class B<T extends C> extends A<T> {
         public B() {}
         public static void main(String[] args) {
              B inst = new B();
              C cInst = inst.foo3();  //problem : it does'nt work !!
    public class C {
         public C() {}
    }The problem is in the main of the class B, the compiler says : "Cannot convert from Object to C".
    My reasoning :
    There is a type constraint (a lower bound) on T in the class B, so "T extends C" in the class B, so "T extends C" in the class A for an instance of the class B. The call to the method foo() does answer an object which class extends class C !!
    Someone has an explanation, it's abnormal or I should set the parameters of javac.
    thank you.

    Ok my examples were wrong !!
    Have a look on this example (smaller and better !)
    B and A are generics class,
    B herits of A and there is a lower bounds on the class T in A "T extends C"
    public class A<T extends C> {
         public A() {}
         public T foo()
         public void foo2() {
              C cinst = foo(); //no problem : it works
    public class B<T> extends A<T> {
         public B() {...}
         public void foo3() {
              C cinst = foo(); //error : "cannot convert T to C"
    }The type constraint "<T extends C>" of the class "A" is not propagated to the class "B" !!
    I have to add the constraint <T extends C> to the class B like this
    public class B<T extends C> extends A<T> {...}However, it's inevitably like this !

  • Bounded int beed help making a test driver  program

    Bounded int here's my code i want to make a test drive program
    getLowerBound that returns the lower bound for the object
    getUpperBound that returns the upper bound for the object
    getValue that returns the current value for the object
    setValue that modifies the value for the object - if the value passed to the method is within the bounds then the value can be changed, however if the value is outside the legal range then the value should not be changed
    public class BoundedInt{
    private static final int intLowerBound = 3;
    private static final int intUpperBound = 10;
    private int value;
    public BoundedInt() {
    this.value = 3;
    public void setValue (int tempValue) {
    if(tempValue < intUpperBound && tempValue > intLowerBound) {
    this.value = tempValue;
    public int getValue () {
         return value;
    public int getLowerBound () {
         return intLowerBound;
    public int getUpperBound () {
         return intUpperBound;
    this is my test drive program (i dont know if i did it right) when i compile it it gives me this error am i doing some thing wrong or i may b missing some line of code ??
    import java.util.*;
    public class testdriver {
    public static void main(String[] args) {
    Scanner scan = new Scanner (System.in);
    BoundedInt b = new BoundedInt(2);
    System.out.println(b);
    out put : testdriver.java:30: cannot find symbol
    symbol : constructor BoundedInt(int)
    location: class BoundedInt
    BoundedInt b = new BoundedInt(2);

    1) When you post code, use the CODE button or [code] and [/code] tags to preserve formatting and make your code readable. Copy/paste from your original source in your editor, NOT from your post here, which has already lost all formatting. Most of us won't even bother reading your post otherwise.
    2) What is your question. Don't say, "How do I make a test driver?"

  • Bounded Wildcard prob

    Hi All,
    Below is what the description says about "super"
    You can also specify a lower bound for a wildcard by adding a super clause to a wildcard declaration. Here is its general form:
    <? super subclass>
    In this case, only classes that are super classes of subclass are acceptable arguments. This is an exclusive clause, because it will not match the class specified by subclass
    However, pls look at the following code. According to the above description the below code(printXY_1(c4) call) should not compile and run, however is is running properly. I am wondering if i've missed something. Pls comment. Thank you in advance.
    class TwoD
         int x,y;
         public TwoD(int a,int b)
              x=a;y=b;
    class ThreeD extends TwoD
         int z;
         public ThreeD(int a,int b,int c)
              super(a,b);
              z=c;
    class FourD extends ThreeD
         int w;
         public FourD(int a,int b,int c,int d)
              super(a,b,c);
              w=d;
    class Coordinates<T extends TwoD>
         T [] coords;
         public Coordinates(T [] o)
              coords=o;
    public class BoundedWildcards
         public static void main(String args[])
              TwoD [] t2={ new TwoD(1,2),new TwoD(4,5)};
              Coordinates<TwoD> c2=new Coordinates<TwoD> (t2);
              ThreeD [] t3={ new ThreeD(1,2,3),new ThreeD(4,5,6)};
              Coordinates<ThreeD> c3=new Coordinates<ThreeD> (t3);
              FourD [] f4={ new FourD(0,1,2,3),new FourD(4,5,6,7)};
              Coordinates<FourD> c4=new Coordinates<FourD> (f4);
              System.out.println("\nPrint X & Y co-ordinates");
              printXY_1(c2);
              printXY_1(c3);
              printXY_1(c4);     }
                        static void printXY_1(Coordinates<? super FourD> obj)
              for(int i=0;i<obj.coords.length;i++)
                   System.out.println("X: "+obj.coords.x+"\tY: "+obj.coords[i].y);

    This is an exclusive clause, because it will not match the class specified by subclassThat doesn't agree with what I have read. (And it doesn't agree with what I would expect, either.) And what I have read is the first 200 pages of Angelika Langer's Generics FAQ document, which repeatedly says that <? super X> includes X. It specifically says that <? super Object> is legitimate but can only be satisfied by Object.

  • JSpinner with bounded dates?

    I'm having trouble implementing a spinner that restricts entry to future dates only (i.e. lower-bound is current time).
    When I use
    Date current = new Date(System.currentTimeMillis());
    Date earliest = new Date(current.getTime());
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, 1);
    Date latest = cal.getTime();
    SpinnerModel m = new SpinnerDateModel(current, earliest, latest, Calendar.MINUTE);the spinner does not spin at all (I expect it to increment the minute digits).
    When I use
    SpinnerDateModel(new Date(System.currentTimeMillis()), null, null, Calendar.MINUTE);the spinner works of course but there are 2 issues:
    1) there's no lower bound
    2) the left-most field is incremented/decremented (i.e. day field in my case)
    By sub-classing BasicSpinnerUI and overriding installNextButtonListeners I get the spinner to work, but this solution is not portable.
    What is the most elegant workaround (that is also portable across OS) for this problem (what seems to be a bug)?
    Help much appreciated,
    Lance

    (I expect it to increment the minute digits).
    [with no bounds] the left-most field is incremented/decremented (i.e. day field in my case)Note that the spinner UI may ignore the increment specified in the SpinnerDateModel constructor : that happens to be the case in my laptop (WinXP, GMT TZ, US display, BasicSpinnerUI L&F), as is expressed in the javadoc for SpinnerdateModel:
    However some UIs may set the calendarField before commiting the edit to spin the field under the cursor. If you only want one field to spin you can subclass and ignore the setCalendarField calls.
    In my case, regardless of the Calendar.MINUTE argument (and I tried also with other values), the field under the cursor is incremented/decremented by the arrow buttons. As the Javadoc says, if you only want minutes to spin, just do that:
            SpinnerModel m = new SpinnerDateModel(current, earliest, end, Calendar.MINUTE) {
                @Override
                public void setCalendarField(int calendarField) {
                    // Don't change the default incremented field
            };Edited by: jduprez on Jun 27, 2009 2:09 PM

  • Autoscale only upper bound

    Is there a way to autoscale only the upper bound of the Y axis on the XY graph.  I would like the lower bound to be fixed at zero.  I'm using LabView 9.0
          Thanks, Vitaliy
    Solved!
    Go to Solution.

    I don't think you can do auto-scale this way, but you can change the axis setting though a vi.  You can change your y axis max to the max of your y values + an offset.
    Yik
    Kudos and Accepted as Solution are welcome!

  • Confidence bounds for non-linear fit

    Is it possible to get the confidence bounds for the fitted coeficients when using non-linear fits?

    Hello Dahl
    Do you mean like Upper Bound and Lower Bound output terminals of NI_Gmath.lvlib::Nonlineat Curve fir intervals.vi?
    Message Edited by Eirikur on 01-09-2008 01:25 PM
    Regards,
    Eirikur Runarsson
    Platinum Applications Engineer
    NI Denmark

  • Bound Mismatch

    Sorry in advance because this is surely a really simple problem you see all the time, but whats wrong with this code...public abstract class DRENode {
        public <T extends Collection<? extends DRENode>> T getAllNodes(T c){
             c.add(this);
             return c;
    }The error I'm getting is at the c.add line and it says..."Bound mismatch: The method add(? extends DRENode) of type T is not applicable for the arguments (DRENode). The wildcard parameter ? extends DRENode has no lower bound, and may actually be more restrictive than argument DRENode"

    What you're saying is that your collection is of some fixed type that extends DRENode. In particular, it might be a collection of a subtype of DRENode, in which case being allowed to put a DRENode in to it would be incorrect.
    I suspect you want <? super DRENode>, or maybe even just <DRENode>

  • Bounded Return Type

    If I have following code:
    public class Cell<X>
       X value;
      public Cell<? super X> copyToAndReturn(Cell<? super X> other)
          other.value = value ;
          return other;
      static void foo()
         Cell<String> c = new Cell<String>();
         Cell<Object> o = new Cell<Object>();
         o = c.copyToAndReturn(o);
    }Then the line:
         o = c.copyToAndReturn(o);gives a compile error:
    Type mismatch: cannot convert from Cell<capture#3-of ? super String> to Cell<Object>
    Object is indeed super class of String. If I have done something wrong in the assignment then what should be the type of assigned variable?

    panopticon wrote:
    The error is in assigning return type to Cell<? super String> to a variable of Cell<Object>.And you are surprised by this why?
    Cell<String> sc = new Cell<String>();
    Cell<? super String> ssc = sc;
    Cell<Object> oc = ssc; // woups..Just because Object is a supertype of String it's not necessarily the supertype represented by the wildcard. If you want more control over the type, use a type variable:
    public <C extends Cell<? super X>> C copyToAndReturn(C other) {
        other.value = value ;
        return other;
    }Sadly the JLS does not allow lower bounds on type parameters, otherwise the solution would be somewhat sexier:
    public <S super X> C copyToAndReturn(Cell<S> other) {
        other.value = value ;
        return other;
    }With kind regards
    Ben

Maybe you are looking for

  • Email ALV report to a distribution list

    Hi all, I have scheduled an alv report (uses ALV Grid) to run in the background using SM36.I put the spool list to be delivered to a set of people whose addresses are in the distribution list. The mails are being received by the users. But the proble

  • Final Cut Pro for PowerPc G3 Mac

    is there Final Cut Pro for PowerPc G3 Mac, Im pretty sure I need version 2.0, so please comment a download like, or comment if you know anything, thanks

  • How not to automatically resize updated images

    I'm stumped... I have all my images placed from illustrator at 100%. When I modify the illustrator files, and update the images in InDesign, not only does it resize (up or down), but it moves its x- and - placement coordinates, so I have to go back a

  • Preloading several files, showing one "% loaded" value

    Hello everyone. For this website I'm developing I need to preload several files (images, xml, css and the main .swf). I have a preloader and I want it to show the total % loaded of the sum of all those files. From what I've learned so far, I need a P

  • Where to dowload table script for demo table EMPLOYEE

    Hello! I'm reading the book "Oracle PL/SQL Programming, Fourth Edition" By Steven Feuerstein. And I can't find the script for the employee table .. Where can I get it? Regards Tobias