0

I have a simple class below which when compiled autoboxes the Integer correctly But, fails to do it for my Boolean it insists that I should change the parameter to a boolean. I am using jdk 1.8 otherwise the compiler would have complained about the Integer conversion. I cannot see what I am doing wrong ? All wrapper classes can autobox out of the box or so I thought ?

public class MsgLog<Boolean,String> {

    private boolean sentOk ;
    private Integer id ;
    private int id2 ;
    public boolean isSentOk() {
        return sentOk;
    }

    public String getTheMsg() {
        return theMsg;
    }

    private String theMsg ;

    private MsgLog(Boolean sentOkp, String theMsg)
    {

        this.sentOk = sentOkp ; // compile error - autoboxing not working

        this.theMsg = theMsg ;

        this.id = 2; // autoboxing working
        this.id2 = (new Integer(7)) ; // autoboxing working the other way around as well

    }

}

And is it not the case that Autoboxing is a two way process ?

Compile error on jdk 8 (javac 1.8.0_25)
Multiple markers at this line
    - Duplicate type parameter String
    - The type parameter String is hiding the type String
    - The type parameter Boolean is hiding the type 
     Boolean
user1561783
  • 483
  • 1
  • 4
  • 14
  • 2
    You might consider sharing the actual compiler error rather than making us guess... – evanchooly May 01 '14 at 13:50
  • 3
    There's no boxing at all going on in the line `this.id = 2;` so your comment "autoboxing working" is not correct. – Jesper May 01 '14 at 13:53

1 Answers1

6

Your problem is the first line:

public class MsgLog<Boolean,String> 

You are declaring type parameters named "Boolean" and "String". These are shadowing the actual Boolean and String types. You don't even need type parameters for this class, as far as I can see; just remove them. If you do want to keep them, you should rename them to avoid shadowing the existing types.

Semantically, the code you posted is equivalent to (with some snipped for brevity):

public class MsgLog<T,U> {

    private boolean sentOk ;
    private U theMsg ;

    private MsgLog(T sentOkp, U theMsg)
    {

        this.sentOk = sentOkp ; // compile error - assignment to incompatible type
        this.theMsg = theMsg ;
    }

}
davmac
  • 20,150
  • 1
  • 40
  • 68
  • +1, specifically they are _generic_ type parameters, which might help clarify it for the OP. – GriffeyDog May 01 '14 at 14:14
  • 1
    @GriffeyDog thanks, but these type parameters are _not_ generic (instead, they are type parameters for a generic type)! The term 'type parameter' is correct in this instance. The language spec does not even use the term 'generic type parameter' as far as I can see. See eg http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.2 – davmac May 01 '14 at 14:47
  • I could have used better wording. They are type parameters of a _generic class_. – GriffeyDog May 01 '14 at 14:55
  • @davmac even if they are shadowing the actual - both the so called covered (shadowed) Boolean and the cover causing Boolean .... both should still behave in the same way i.e. be in a position to Autobox ?? – user1561783 May 01 '14 at 16:30
  • 1
    @user1561783, no. Autboxing won't work on some arbitrary type parameter, which is what you now have. Eg. replace 'Boolean' in your example with 'T', everywhere it occurs (including in the method signature). This is semantically equivalent. Would you still expect it to autobox/unbox? In any case, if you remove the type parameters from the class, you will see that it compiles. This should be proof enough. – davmac May 01 '14 at 21:54
  • @user1561783 I have amended the answer, I hope this makes sense to you. – davmac May 01 '14 at 22:07