This discussion is archived
1 2 Previous Next 16 Replies Latest reply: Oct 16, 2008 1:52 AM by 3004 RSS

Bounded Type Parameter with Boolean

843785 Newbie
Currently Being Moderated
class Gen<M extends Boolean>
{
 M x;
 Gen(M x) {this.x = x;}
};
class TestGeneric
{
 public static void main(String aa[])
 {
  Gen<Boolean> ob = new Gen<Boolean>(true);
 }
}
When you try compiling the above class, Compiler will report the follwing error message :


TestGeneric.java:10: cannot find symbol
symbol : method valueOf(boolean)
location: bound of type variable M
Gen<Boolean> ob = new Gen<Boolean>(true);
^
Fatal Error: Unable to find method valueOf

Can anyone explain...

  • 1. Re: Bounded Type Parameter with Boolean
    801484 Newbie
    Currently Being Moderated
    class Gen<M extends Boolean>
    {
      M x;
      Gen(M x) {this.x = x;}
    }
    
    class TestGeneric
    {
      public static void main(String aa[])
      {
        //Gen<Boolean> ob = new Gen<Boolean>(true); //You are passing value here
          Gen<Boolean> ob = new Gen<Boolean>(Boolean.valueOf(true)); //Here we are passing the Boolean type of  Instance
    
         Gen<Boolean> ob2 = new Gen<Boolean>(new Boolean(true)); //This is also valid
       }
    }
    Edited by: j2ee_ubuntu on Oct 15, 2008 12:44 AM
  • 2. Re: Bounded Type Parameter with Boolean
    800282 Newbie
    Currently Being Moderated
    This:
    class Gen<M extends Boolean>
    is of no use: Boolean is final so you can extend it.

    Perhaps you meant ot do something like this
    class Gen<T>
    {
      T type;
      Gen(T type) { this.type = type; }
    }
    
    // ...
    
    Gen<Boolean> ob = new Gen<Boolean>(true);
    ?
  • 3. Re: Bounded Type Parameter with Boolean
    801484 Newbie
    Currently Being Moderated
    prometheuzz wrote:

    is of no use: Boolean is final so you can extend it. (italic word is replaced by cant't)

    Edited by: j2ee_ubuntu on Oct 15, 2008 1:15 AM
  • 4. Re: Bounded Type Parameter with Boolean
    800282 Newbie
    Currently Being Moderated
    j2ee_ubuntu wrote:
    prometheuzz wrote:

    is of no use: Boolean is final so you can extend it. (italic word is replaced by cant't)
    Ah yes, I meant "can't". Thanks.
  • 5. Re: Bounded Type Parameter with Boolean
    843785 Newbie
    Currently Being Moderated

    j2ee_ubuntu, Thanks for the reply. I still have some doubts

    1. First, why autoboxing of value true is not automatic here
    2. Second, why compiler is complaining of non-availability of method valueOf(+boolean+) at the location (bound of type variable M).
    According to my understanding, the bound of type variable M is Boolean, and it does have a method valueOf(+boolean+)

  • 6. Re: Bounded Type Parameter with Boolean
    843785 Newbie
    Currently Being Moderated

    prometheuzz, thanks again. Still some points...

    I am not extending Boolean class. I am just applying upper bound to the type variable, as I don't want this generic class to be used with any other type. True, it will nullify the purpose of using Generic, as the class will be used with just one type - Boolean. But, I don't think anything syntactically wrong here (Correct me, if I am wrong). The big point is why boxing is not automatic here.

  • 7. Re: Bounded Type Parameter with Boolean
    801484 Newbie
    Currently Being Moderated
    Tejaswi wrote:
    
    According to my understanding, the bound of type variable M is Boolean, and it does have a method valueOf(boolean)
    No it's not. Boolean is a final class. Parameter M is not bounded by the final type Boolean.

    M doesn't have a method valueOf(Boolean)
    Since program fails to compile thatswhy autoboxing fails.

    Edited by: j2ee_ubuntu on Oct 16, 2008 12:37 AM
  • 8. Re: Bounded Type Parameter with Boolean
    3004 Newbie
    Currently Being Moderated
    j2ee_ubuntu wrote:
    Tejaswi wrote:
    
    According to my understanding, the bound of type variable M is Boolean, and it does have a method valueOf(boolean)
    No it's not. Boolean is a final class. Parameter M is not bounded by the final type Boolean.
    Eh? Can you elaborate?
  • 9. Re: Bounded Type Parameter with Boolean
    800282 Newbie
    Currently Being Moderated
    Tejaswi.Khijwania wrote:

    prometheuzz, thanks again. Still some points...

    I am not extending Boolean class. I am just applying upper bound to the type variable, as I don't want this generic class to be used with any other type. True, it will nullify the purpose of using Generic, as the class will be used with just one type - Boolean. But, I don't think anything syntactically wrong here (Correct me, if I am wrong).
    That is correct, you can do it like that, but why would you choose such a solution if this is equivalent:
    class Gen {
        Boolean x;
        Gen(Boolean x) {this.x = x;}
    }
    ?

    Unless the code you provided is only for testing purposes, but you didn't mention that fact.
    Tejaswi.Khijwania wrote:

    The big point is why boxing is not automatic here.
    I don't have Sun's compiler (javac) here at the moment, but Eclipse's compiler compiles, and then runs this (with Sun's JRE) without a problem:
    public class Test {
        public static void main(String[] args) {
            Gen<Boolean> ob = new Gen<Boolean>(true);
        }
    }
    
    class Gen<M extends Boolean> {
        M x;
        Gen(M x) {this.x = x;}
    }
  • 10. Re: Bounded Type Parameter with Boolean
    800282 Newbie
    Currently Being Moderated
    Tejaswi.Khijwania wrote:
    ... The big point is why boxing is not automatic here.
    This seems to be a known javac 1.5 (and maybe 1.6?) bug:
    [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6355696]
  • 11. Re: Bounded Type Parameter with Boolean
    3004 Newbie
    Currently Being Moderated
    Tejaswi.Khijwania wrote:
    class Gen<M extends Boolean>
    {
    M x;
    Gen(M x) {this.x = x;}
    };
    class TestGeneric
    {
    public static void main(String aa[])
    {
    Gen<Boolean> ob = new Gen<Boolean>(true);
    }
    }
    When you try compiling the above class, Compiler will report the follwing error message :


    TestGeneric.java:10: cannot find symbol
    symbol : method valueOf(boolean)
    location: bound of type variable M
    Gen<Boolean> ob = new Gen<Boolean>(true);
    ^
    Fatal Error: Unable to find method valueOf

    Can anyone explain...

    I gotta admit, I don't see why it's a problem. Boolean being final shouldn't matter. Either the compiler should complain and require you to use just <Boolean> instead of <M extends Boolean>, or it should let it go, since the fact that nothing actually extends Boolean won't negatively affect generics.

    Boolean has a public valueOf(boolean) method, so any M that extends Boolean also has that method.

    It works with Boolean.TRUE in place of true, so it's clearly something with boxing, but I'm not seeing it.
  • 12. Re: Bounded Type Parameter with Boolean
    801484 Newbie
    Currently Being Moderated
    jverd wrote:
    
           Eh? Can you elaborate?
    Sorry for my Explanations. I wrote what i get in my Eclipse IDE

    type parameter M should bot be bounded by the final type Boolean. Final types cant be further extended.

    so i extract that meaning:(

    But you clear the things very clear. Thanks For that.
  • 13. Re: Bounded Type Parameter with Boolean
    800282 Newbie
    Currently Being Moderated
    j2ee_ubuntu wrote:
    ...
    type parameter M should bot be bounded by the final type Boolean. Final types cant be further extended.
    ...
    That's a warning from Eclipse (not an error), but it's still valid to write it like that.
  • 14. Re: Bounded Type Parameter with Boolean
    800308 Newbie
    Currently Being Moderated
    My (soon to be toally worthless) two bobs worth:

    Why would want to write a generic class <M extends ${finalClass}> when the equivalent is a simple non-generic class?

    Everything should be kept as simple as possible, but not simpler.
    ~~ Einsten

    If you handed me that +<M extends ${finalClass}>+ class I'd have to waste my time working out "what gives"... and I'm sure I'm not alone. It even seems to have exposed a 1.5 compiler bug... Sheesh!

    This code idiom may have some intellectual value (which I am, apparently, far too stupid to grasp)... but I assure you that it has zero practical value, and if I picked it up in a code review I'd summarily sack the author...

    Life is no fun since the HR department traded-in there bamboo scures, and nipple clamps ;-)

    Cheers. Keith.
1 2 Previous Next